WordPress.org

Ready to get started?Download WordPress

Forums

Per Page Widgets
modification to add per page widgets to custom post types (2 posts)

  1. aleonzzz
    Member
    Posted 1 year ago #

    if you edit the main plugin file i123_per_page_widgets.php line 196

    Change the function i123_widgets_custom_fields_add() as below and the widgets chooser will show up in your custom post types as well.

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    function i123_widgets_custom_fields_add() {
        i123_widgets_admin_init();
        add_meta_box( 'i123_widgets_custom_fields', __('Per Page Widgets', 'i123_widgets'), 'i123_widgets_custom_fields_controllbox', 'post', 'side', 'high' );
        add_meta_box( 'i123_widgets_custom_fields', __('Per Page Widgets', 'i123_widgets'), 'i123_widgets_custom_fields_controllbox', 'page', 'side', 'high' );
    	$args=array(
    		'public' => true,
    		'_builtin' => false
    	);
    	$operator = "and"; //'and or 'or'
    	$output = "names"; //names or objects - names is default
    	$post_types = get_post_types($args,$output,$operator);
    	foreach($post_types as $post_type) {
    		add_meta_box( 'i123_widgets_custom_fields', __('Per Page Widgets', 'i123_widgets'), 'i123_widgets_custom_fields_controllbox', $post_type, 'side', 'high' );
    	}
    }

    http://wordpress.org/extend/plugins/per-page-widgets/

  2. katwhite
    Member
    Posted 4 months ago #

    This is the right idea, but there is a better approach.

    Instead of editing the plugin file, use the WordPress action hook add_meta_boxes in your functions.php file or custom plugin. This way the boxes get added and you won't run into upgrade issues on the plugin.

    Here's the adapted implementation:

    function my_custom_meta_boxes( $post_type, $post ) {
    	$args = array(
    		'public' => true,
    		'_builtin' => false
    	);
    	$operator = "and"; //'and or 'or'
    	$output = "names"; //names or objects - names is default
    	$post_types = get_post_types($args,$output,$operator);
    
    	foreach($post_types as $post_type) {
    		add_meta_box( 'i123_widgets_custom_fields', __('Per Page Widgets', 'i123_widgets'), 'i123_widgets_custom_fields_controllbox', $post_type, 'side', 'high' );
    	}
    }
    
    add_action( 'add_meta_boxes', 'my_custom_meta_boxes', 10, 2 );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic