0

I have a plugin that converts hashtags (any text that starts with #) into Wordpress tags. It's working great when I manually create a post via the Wordpress dashboard, but doesn't work when I programmatically create a post. Here's how I'm programmatically creating the posts:

First I create a draft:

$post = array(
  'post_content'   => $messagefordatabase,
  'post_name'      => $publickey,
  'post_title'     => $publickey,
  'post_status'    => 'draft'
);

$post_id = wp_insert_post($post);

Then when it's time to publish the post, this code changes the draft's publish status. (This publishes the post, but doesn't run the plugin):

wp_publish_post($postid);

And lastly, here's the code from the plugin:

function hashtags_insert_post( $id, $data ) {
  if (preg_match_all("/".HASHTAGS_REGEXP."/i", $data->post_content, $match)) {
    $tags = implode(", ", $match[2]);

    wp_add_post_tags( $data->post_parent, $tags );
  }

  return $id;
}
add_filter( 'wp_insert_post', 'hashtags_insert_post', 99, 2 );

So in summary, the first 2 chunks of code above don't run the plugin. How can I trigger the plugin from either of those 2 chunks of code? Thanks!!

  • At which point during load are you creating the posts exactly? – Rarst Jun 19 '14 at 20:09
  • The first piece of code above creates the post as a draft. Then the second piece of code changes the draft's status to published. – user1399181 Jun 19 '14 at 20:31
  • I understand what they do. But when and how do you run them? Is it in normal page? In Ajax request? In cron? On load? On init? After init? – Rarst Jun 19 '14 at 20:36
  • The first 2 chunks are run from 2 separate page templates. Does that help? (Thanks for your help so far, btw!) – user1399181 Jun 19 '14 at 20:51
  • Hm... As described it should work then. Try to elaborate on details in the question. – Rarst Jun 19 '14 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.