0

I'm trying to make some plugin. This plugin is some kind of auto posting.

function new_post($title,$content)
{
$post = get_post($id);
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$title = $post->post_title;
$content = $post->post_content;
echo $title;
echo $content;
}
add_action( 'publish_post', 'new_post' );

I can see only $title, $content is empty.

I've checked mysql in draft status, I can see post_title fields has data but post_content has no data.

post_content has data when I click publish or auto save. I think this plugin run faster than wordpress insert data process.

This plugin have to run after publish or make different way.

How can I fix this code...

I hope wordpress developer's can help.

2
  • 1
    Sorry, I can't tell what you are trying to do. What is the point of that code? What should it accomplish?
    – s_ha_dum
    Commented Mar 19, 2014 at 14:48
  • Where is $id coming from when looking for a post?
    – Howdy_McGee
    Commented Mar 19, 2014 at 15:29

1 Answer 1

1

First, you need to pass the post $id in the function and not $title and $content:

function new_post($id)

Then your function may be echoing the content - check your source code in your browser via "View Page Source" if it appears blank.

However, if you use echo $content then shortcodes, WordPress's formatting and password protection functions will all be broken. You need to wrap your call like this:

echo apply_filters('the_content', $post->post_content);

More info on this here and here

function new_post($post_ID)
{
$post = get_post($post_ID);
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$title = $post->post_title;
$content = $post->post_content;
echo $title;
echo apply_filters('the_content', $content);
}
add_action( 'publish_post', 'new_post' );

Also, if you're trying to grab the updated/changed info it will be stored in the $_POST variable. But if you grab info from $post->post_content that is the old info already stored in the database from before you save/publish/update your post. At some point you may also want a conditional statement to check if this is the first time you are publishing the page, or if it is being updated. Something like this:

if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
// code to execute here...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.