WordPress.org

Ready to get started?Download WordPress

Forums

Custom Author Metabox Saving Data Problem (3 posts)

  1. toneburst
    Member
    Posted 1 year ago #

    Hi,

    I'd like to modify the content of the Author metabox in on the Page/Post Edit pages. I can't see any hooks specifically for this in meta-boxes.php or post.php, so I'm assuming I'll have to remove the metabox and add my own modified version.

    I've managed to do this, but I'm not sure how to save the value from the dropdown. I'd assumed that if I simply copied the markup from the original Author metabox, then the data would automatically be saved with the post. However, this doesn't appear to be the case- I guess because WordPress disables saving of post author data when the Author panel is removed.

    This is where I am at the moment:

    <?php
    /**
     *
     *
    */
    
    class ell_usertools_editpanel {
    
    	public function __construct() {
    
    		$this->ellut_utils = new ell_usertools_utils();
    
    		///////////////////////////////////////
    		// Remove standard Author edit panel //
    		///////////////////////////////////////
    
    		add_action( 'admin_menu', array( &$this, 'ellut_remove_author_meta_box' ) );
    
    		//////////////////////////////////
    		// Add modified Author meta box //
    		//////////////////////////////////
    
    		add_action( 'add_meta_boxes', array( &$this, 'ellut_add_author_meta_box' ) );
    
    		///////////////////////////////////////////
    		// Save value from modified Author panel //
    		///////////////////////////////////////////
    
    		add_action( 'save_post', array( &$this, 'ellut_save_postdata' ) );
    	}
    
    	public function ellut_remove_author_meta_box() {
    		http://codex.wordpress.org/Function_Reference/remove_meta_box
    		if( current_user_can( 'edit_others_posts' ) ) {
    			remove_meta_box( 'authordiv', 'post', 'normal' );
    		}
    	}
    
    	public function ellut_add_author_meta_box() {
    		http://codex.wordpress.org/Function_Reference/add_meta_box
    		add_meta_box( 'ellut_author_meta_box', 'Author (Modified)', array( &$this, 'ellut_render_author_meta_box' ), 'post', 'advanced', 'high');
    	}
    
    	public function ellut_render_author_meta_box() {
    		global $user_ID;
    		?>
    		<label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
    		<?php
    			wp_dropdown_users( array(
    				'who' => 'authors',
    				'name' => 'post_author_override',
    				'selected' => empty($post->ID) ? $user_ID : $post->post_author,
    				'include_selected' => true
    			) );
    		?>Extra content<?php
    	}
    
    	public function ellut_save_postdata( $post_id ) {
    
    		//$this->ellut_utils->log_me( $_POST );
    
    		if (!empty( $_POST['post_author_override'] ) ) {
    
    			// http://codex.wordpress.org/Function_Reference/wp_update_post
    			if ( !wp_is_post_revision( $post_id ) ) {
    
    				// Unhook this function so it doesn't loop infinitely
    				remove_action('save_post', array( &$this, 'ellut_save_postdata' ) );
    
    				// update the post, which calls save_post again
    				$my_post = array();
    				$my_post['ID'] = post_id;
    				$my_post['post_author'] = (int) $_POST['post_author_override'];
    				wp_update_post( $my_post, TRUE );
    
    				// Re-hook this function
    				add_action( 'save_post', array( &$this, 'ellut_save_postdata' ) );
    			}
    		}
    	}
    }
    ?>

    Everything appears to work, except that the the value from the dropdown does not appear to be saved when the post is saved/updated. Can anyone tell me where I'm going wrong? I was able to log $_POST, so I know that $_POST['override_author'] is being set. It's probably a typo or something, but I've not been able to spot it.

    Any help much appreciated.

    Cheers,

    Alex

  2. ianhaycox
    Member
    Posted 1 year ago #

    In ellut_render_author_meta_box() use

    global $user_ID, $post;

    because $post is not defined, so it always selects the current user (admin).

    Always define('WP_DEBUG', true); in wp-config.php, wind up PHP error reporting http://php.net/manual/en/function.error-reporting.php and check the server error log.

    The save works OK

    Ian.

  3. toneburst
    Member
    Posted 1 year ago #

    Hi Ian,

    thanks very much for getting back to me. That does the trick perfectly!

    Re. error-reporting; I have turned PHP error reporting on in the WordPress config file, as you suggest, but haven't really been making use of it. I must start to do that. I've been logging stuff to the WordPress error log though, which has proved useful (you can see the call the the 'log_me' method of the ellut_utils class instance in the code above).

    Thanks very much once again!

    Alex

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.