WordPress.org

Support

Support » Plugins and Hacks » Hacks » $post variables aren't outputting any data inside custom plugin.

$post variables aren't outputting any data inside custom plugin.

  • How can I use $post variables inside a custom plugin?

    I’ve tried:

    global $post;
    echo $post->post_title;

    and:

    the_ID();

    But none of them output any data. How can those variables work inside a custom plugin? Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Grizzam,

    Using the global variable is correct however it sounds like you are using it outside of the post loop.

    global $post;
    var_dump( $post );

    If it shows null you are outside of the loop.

    This probably means your function is running on the init action.
    Here is an example of some code that will work.

    add_action( 'the_post', array( &$this, 'your_function') );
    
    function your_function(){
       global $post;
       var_dump( $post );
    }

    Hope that helps.

    Hi dlngle,

    Thanks for the response. I added this inside a custom plugin:

    add_action( 'the_post', array( &$this, 'get_post_info') );
    
    	function get_post_info() {
    	    global $post;
    	    var_dump( $post );
    	    }

    But, I get this error:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /Applications/XAMPP/xamppfiles/htdocs/wp-includes/plugin.php on line 507

    That code is literally all I have in the plugin. It gives that error when I go into any post. I literally just want to be able to pull data about the current post. Any ideas?

    Ok, I assumed this code was going to appear inside a class.

    That’s ok just remove
    array( &$this, 'get_post_info' )
    and replace with
    'get_post_info’`

    Because you are not in a class you do not need to send the object across ( And there is no object ).

    Final Code:

    add_action( 'the_post', 'get_post_info' );
    
    	function get_post_info() {
    	    global $post;
    	    var_dump( $post );
    	    }

    Hope that works

    That works! I have this code:

    add_action( 'the_post', 'get_post_info' );
    
    	function get_post_info() {
    	    global $post;
    	    echo $post->ID;
    	    }

    It outputs this though: get_post_info(); 10

    The ID the the post I was on is 10, so it’s retrieving the variables I want. But it’s also outputting the function name. How do I get rid of that?

    Make sure you don’t have get_post_info(); outside of php tags somewhere.
    The code you are showing me will not print out the function name.

    Check you’re not printing or echoing it either.

    Also that hook will be called whenever wp goes to loop through posts.

    use:

    add_action('wp','myfunction');
    function myfunction(){
    	    $post = get_post();
                var_dump($post);
    }

    The above code will only output the data once on the page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘$post variables aren't outputting any data inside custom plugin.’ is closed to new replies.
Skip to toolbar