osompress / genesis-portfolio-pro

Plugin: Genesis Portfolio Pro
24 stars 14 forks source link

A way to set posts_per_page on theme activation #19

Closed nickcernis closed 7 years ago

nickcernis commented 7 years ago

We'll need a way to change the new posts_per_page portfolio archive setting upon theme activation. It has to work:

  1. When the theme is activated before the plugin.
  2. When the plugin is activated before the theme but the default has not been edited.
  3. When the plugin is activated before the theme and the default has been edited.

Adding this code to the theme works if the plugin is already active when the new theme is activated:

add_action( 'after_switch_theme', 'genesis_sample_theme_setting_defaults' );
/**
 * Updates theme settings on activation.
 *
 * @since 2.2.3
 */
function genesis_sample_theme_setting_defaults() {
    // Set portfolio items per page if Genesis Portfolio Pro is active.
    if ( defined( 'GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX' ) ) {
        $option_name = GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . 'portfolio';
        $portfolio_options = get_option( $option_name, array() );
        $portfolio_options['posts_per_page'] = 9;
        update_option( $option_name, $portfolio_options );
    }
}

But it won't work if the plugin is not yet active.

Could we drop the GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX from the plugin and hard-code the option name instead? That way we could do this and the option will be set even if the plugin isn't active, ready for the first time it's activated.

add_action( 'after_switch_theme', 'genesis_sample_theme_setting_defaults' );
/**
 * Updates theme settings on activation.
 *
 * @since 2.2.3
 */
function genesis_sample_theme_setting_defaults() {
    // Set portfolio items per page for Genesis Portfolio Pro.
    $portfolio_options = get_option( 'genesis-cpt-archive-settings-portfolio', array() );
    $portfolio_options['posts_per_page'] = 9;
    update_option( 'genesis-cpt-archive-settings-portfolio', $portfolio_options );    
}

Paging @NicktheGeek as you may have smarter ideas about how this should work.

NicktheGeek commented 7 years ago

@nickcernis I'd rather keep the constant in the plugin because if that constant is changed in Genesis, the plugin will catch it. The plugin isn't defining this, it is defined in Genesis.

As such, the constant should be available as long as Genesis is.

nickcernis commented 7 years ago

Thanks, @NicktheGeek — I missed that it was a Genesis constant.

@dreamwhisper Looks like GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX can be assumed for Genesis themes, so theme devs can do this:

add_action( 'after_switch_theme', 'sp_theme_portfolio_pro_defaults' );
/**
 * Update Portfolio Pro posts per page setting.
 *
 * @since 1.0.0
 */
function sp_theme_portfolio_pro_defaults() {
    $portfolio_options = (array) get_option( GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . 'portfolio' );
    $portfolio_options['posts_per_page'] = 9;
    update_option( GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . 'portfolio', $portfolio_options );
}

I tested this and it's working in the above scenarios with 1.1 for me.

If that seems okay, I think it's fine to close this.

dreamwhisper commented 7 years ago

I didn't try it, but it looks ok. Thanks @nickcernis and @NicktheGeek!