Support » Plugins » Hook Publish Post

  • Resolved HNordin



    I’m relative new to wordpress and have currently hit a roadblock.

    What I’m trying to do:

    • filter the content when someone is trying to publish/update a post
    • search for a <!–more–> tag
    • insert the tag if not found

    This is quite done. My problem lies in the hook for the publish button. I tried with adding a filter to wp_insert_post, but I doubt I really understand how hooks work. Even after reading the codex information on them.

    Can someone please tell me in detail (or in general depending on your free time) how to add a filter/action to the “Publish Post/Update Post” button?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Are you doing this within a plugin?

    The way to set up a filter hook handler is to write a function to be the handler, which should accept the relevant arguments for that filter.

    Then you use the add_filter() function to register that handler on the filter hook. Like this:

    function filter_post( $data , $postarr )
      // Do stuff...
      return ( $data )
    add_filter ( 'wp_insert_post_data' , 'filter_post' , 99 );

    wp_insert_post_data is the filter hook you need to use.

    You’ll have to have a look at wp-includes/post.php for information on the parameters, as they’re a bit to complex to include here and the information’s not in the codex (i may add it though). You can probably ignore $postarr though, and just deal with $data.

    $data = array( 'post_author', 'post_date', 'post_date_gmt',
    'post_content', 'post_content_filtered', 'post_title', 'post_excerpt',
    'post_status', 'post_type', 'comment_status', 'ping_status',
    'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified',
    'post_modified_gmt', 'post_parent', 'menu_order', 'guid' )

    Thanks alot!

    That really helped me out. Although I guess my question has been answered, I would really need some fast input before you leave this thread.

    function content_rewrite($content) {
    	$search_string = "<!--more-->";
    	$def_length = 350;
    	$s_start = "";
    	$s_end = "";
    	if (str_pos($content, $search_string) === false) {
    		$s_start = substr($content,0,$def_length);
    		$s_end = substr($content,$def_length);
    		return ($s_start.$search_string.$s_end);
    	} else {
    		return $content;

    I’ve gone through it and made some changes I dont really think are necessary, like the stringvar declarations, and I just can’t find the error. I’m pretty sure it lies in the syntax since I’m new to php and have mostly worked in C# as of before.

    I’m pretty sure it has something to do with concat of the strings. didn’t really give me more help than that you use dots instead of addition-signs, which I tried at first.

    I know the hook is working since i tried with a dummy-pass-through function, which only added a simple string at the end, and it worked.

    Any thought?

    It would help if you said what the problem was! What happens when you run it? Have you looked at the httpd error log to see what PHP errors are being reported?

    [post deleted by author]

    Right. How clumsy of me. Didn’t even think about checking the log. Found the error which indeed was a syntax-a-like error: str_pos() should infact have been strpos().

    Thanks again willkemp.

    The problem is now solved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hook Publish Post’ is closed to new replies.