WordPress.org

Support

Support » Plugins and Hacks » How to do things to POST_TITLE when a post is PUBLISHED?

How to do things to POST_TITLE when a post is PUBLISHED?

  • Hi,

    I am trying to create a plugin which does certain things to the content of a post title when the post is published. Anyway, I can’t find a way to hook into this.
    Any ideas?
    It is going to be a very useful and cool plugin 🙂

    Thanks.

    p.s. different approach to the same question: How to you alter the POST REQUEST as soon as it is sent from the post editor? (if possible?) By adding an action to publish_post, edit_post? or by adding a filter to those functions instead? Or should I hook another function?
    Wordpress basics, i know…

Viewing 11 replies - 1 through 11 (of 11 total)
  • I think it would, but the following doesn’t work:

    function for_example() {
    
         $_POST['post_title'] = 'test';
    
    }
    add_filter('publish_post', 'for_example');

    Also this

    function for_example() {
    
      $my_post = array();
      $my_post['ID'] = $id;
      $my_post['post_title'] = 'test';
    
      // Update the post into the database
      wp_update_post( $my_post );
    	return $id;
    }
    add_filter('publish_post', 'for_example');

    doesn’t work. Still looking for help…

    Put global $_POST; in your function before you try to access the $_POST variable. I’m not sure if that’s ‘best practice’ but it works.

    Actually, your suggestion sounds right but it does not work for me. post_title is not changed upon publication. Do you think the filter should be added to some other function?

    ‘publish_post’ is a action not a filter so you should be using ‘add_action’ rather than ‘add_filter’.

    Also, take a look at the action reference and make sure you are using the hook you need. ‘publish_post’ fires only ‘when a page is published, or if it is edited and its status is “published”‘. It won’t fire when you save a draft, for example. You might want ‘save_post’ instead.

    A couple of things to think about.

    Your first function might work if you call it it before WP processes the $_POST data. Calling it on ‘publish_post’ is too late. I’m not exactly sure which hook you’d want to use to do that– maybe ‘init’.

    Your second functions looks to be failing because $id is empty. At least I can’t see where its getting a value from. Try this instead (assuming you do want ‘publish_post’ rather than some other hook).

    function for_example($id) {
      $my_post = array();
      $my_post['ID'] = $id;
      $my_post['post_title'] = 'test';
      // Update the post into the database
      wp_update_post( $my_post );
    }
    add_action('publish_post', 'for_example');

    using add_action ('init' finally worked with the first function! So thanks a million man.

    anyway, problem: if I use ‘init’ the first function makes no distinction between publishing or editing. I wanted this function to happen only when the post is published.
    But I guess this could be worked out by checking on other $_POST values such as ‘publish’ or ‘post_status’.

    Yet, for the sake of exploring possibilities, the second function should be better for my case….
    Making use of $id (I forgot to put $id in the example here like you noted but it was present in my code when I tested it) it could alter the title after $_POST and therefore be hooked into other actions other than ‘init’. My experiments:

    it does not work with 'publish_post', 'wp_update_post' or 'init' itself.

    strangely enough, 'save_post' seem to work but it causes Apache to crash down!

    'wp_insert_post' creates a infinite series of empty posts titled ‘test’ until apache breaks down.

    'edit_post' duplicates the post you’re editing ad libitum until apache breaks down!!

    I guess I’ll work with the first… 🙂

    You are on the right track with the first function, if you choose to go that route, so I’m going to skip it.

    I’m puzzled about the other failures though. It looks like you are having an infinite loop problem and I’m not sure why. I’m curious. When you use ‘wp_update_post’ Apache crashes, but does the post update the way you want it too? If it does, that might indicate an infinite loop too. But I can’t see why. Is there anything else in your plugin that might be firing? Or in some other plugin that you are using? Turn everything else off and see if your code works.

    You need to do some debugging. Comment out the wp_update_post line in your function. Then echo $id and print_r($my_array). My apologies if you’ve already done this. Are you getting the values you want?

    When I use add_action('wp_update_post nothing happens.
    There is a loop of infinite posts being created when I use add_action(wp_insert_post, while in other cases no additional posts seems to be created, but Apache crashes anyway.

    As I said, save_post does what it is supposed to do (the post title is changed) but then Apache crashes (I am obviously talking about my local XAMPP Apache).

    $my_array works fine, it is what wp_update-post does that isn’t clear to me…

    anyway, in case you are interested: the first draft of my plugin is here. I welcome feedback! 🙂

    But you shouldn’t be using add_action('wp_update_post') or add_action(wp_insert_post') at all. Those aren’t action hooks. Maybe that has been your problem. You can’t call just any function with add_action(). You can only call action hooks that way unless I am very badly wrong.

    The plugin is a good idea, by the way.

    …but on the codex those are listed among the actions you can hook in plugins, here

    Yep, you are right. ‘wp_insert_post’ is listed. Sorry. I got that one wrong. (Thanks for pointing that out, actually. I think it just solved a problem that has been puzzling me.)

    I can’t find ‘wp_update_post’ listed though which explains why it does nothing (and since I am now paranoid about screwing up a second time I looked extra hard).

    I grep-ed my entire installation and the only two place I see where ‘wp_insert_post’ is called with do_action() is in wp-includes/post.php on lines 1542 and 1631, if that helps. Several other times it is called like any other function– probably the source of my confusion.

    I also stumbled over a filter called ‘wp_insert_post_data’ which might have been what you were looking for all along.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to do things to POST_TITLE when a post is PUBLISHED?’ is closed to new replies.
Skip to toolbar