Skip to:
Content
Pages
Categories
Search
Top
Bottom

Code to create topic when post is published. Working great, but one question.


  • ghoush
    Participant

    @ghoush

    I have searched and put code together and tested. Now whenever a new post is created in WordPress a new topic is created in a specific sub-forum on my bbPress. This is working great. It brings over the tags and everything.

    The one problem I have is that the publish_post action fires every time a post is set to published. So if a post gets set back to draft or pending review, and then published again, another forum topic is created. There are problaby 2097324907629306 ways this can be solved. I am only coming up with some pretty stupid ones I think, so I was hoping someone might have a good idea here.

    Here is the code for your functions.php:

    
    function post_published_create_topic( $ID, $post ) {
        $author = $post->post_author;
        $name = get_the_author_meta( 'display_name', $author );
        $title = $post->post_title;
        $permalink = get_permalink( $ID );
        $excerpt = $post->post_excerpt;
        $subject = sprintf( '%s: %s', $name, $title );
        $message = sprintf ('[b]%s[/b]' . "\n" . 'By %s' . "\n\n" . '[i]%s[/i]' . "\n\n" . 'View: %s' . "\n\n", $title, $name, $excerpt, $permalink );
        $tags = wp_get_post_tags( $ID, array( 'fields' => 'names' ) );
    
        $new_post = array(
            'post_title'    => $subject,
            'post_content'  => $message,
            'post_parent' => 691,
            'post_status'   => 'publish',
            'post_type' => 'topic'
        );
        $topic_meta = array(
        'forum_id'       => $new_post['post_parent']
        );
        $topic_id = bbp_insert_topic($new_post, $topic_meta);
        $term_taxonomy_ids = wp_set_object_terms( $topic_id, $tags, 'topic-tag' );
    }
    add_action( 'publish_post', 'post_published_create_topic', 10, 2 );
    

    Take that code and put it in your themes functions.php (hopefully you are using a child theme.) The 691 is my sub-forums ID, replace that with the right ID for yours. The format of the post and its content is done on the line that starts “$message = sprintf”.

    Now I just need to get it to stop creating a new post every time the same post is published. So far my ideas have been something like matching up the topics title with a string comparison, but that seems a bit heavy. There should be a simpler way and I am just not seeing it right now.

Viewing 11 replies - 1 through 11 (of 11 total)

  • ghoush
    Participant

    @ghoush

    An update:

    Here is the fixed up code, all working now. This version creates a meta value for the post and checks that next time it gets published. No more duplicates if something goes published->draft/pending->published again.

    
    function gh_post_published_create_topic( $ID, $post ) {
        $checkduplicate = get_post_meta( $ID, 'gh_topic_created',true);
        if ( empty( $checkduplicate ) ) {
            $author = $post->post_author;
            $name = get_the_author_meta( 'display_name', $author );
            $title = $post->post_title;
            $permalink = get_permalink( $ID );
            $excerpt = $post->post_excerpt;
            $subject = sprintf( '%s: %s', $name, $title );
            $message = sprintf ('[b]%s[/b]' . "\n" . 'By %s' . "\n\n" . '[i]%s[/i]' . "\n\n" . 'View: %s' . "\n\n", $title, $name, $excerpt, $permalink );
            $tags = wp_get_post_tags( $ID, array( 'fields' => 'names' ) );
    
            $new_post = array(
                'post_title'    => $subject,
                'post_content'  => $message,
                'post_parent' => 691,
                'post_status'   => 'publish',
                'post_type' => 'topic'
            );
            $topic_meta = array(
                'forum_id'       => $new_post['post_parent']
            );
            $topic_id = bbp_insert_topic($new_post, $topic_meta);
            if ( !empty( $topic_id ) ) {
                add_post_meta($ID, 'gh_topic_created', 'true', true);
                $term_taxonomy_ids = wp_set_object_terms( $topic_id, $tags, 'topic-tag' );
                bbp_update_forum(array('forum_id' => 691));
            }
        }
    }
    add_action( 'publish_post', 'post_published_create_topic', 10, 2 );
    

    ghoush
    Participant

    @ghoush

    Just for people who don’t notice it. The forum id is in two places now, so replace both 691’s with your own forum id.


    Leng7881
    Participant

    @leng7881

    This is probably a stupid question but where do I get my forum id?


    Robkk
    Moderator

    @robkk

    @leng7881

    Install a plugin like Reveal IDs, and when you go to Forums > All Forums in the WordPress backend you will see all the forum IDs next to each forum.

    There are other ways of getting a posts ID, but this is the easiest way.


    Leng7881
    Participant

    @leng7881

    I got the id from the Reveal ID’s thank you for that. I am trying to get this to work on my website which currently is on a localhost so can’t send you to it. I have put the code in the functions and changed the number to 72 (my forum id), however, when I post it does not create a new topic in that forum. I have been poking around trying to get it to work and have found that some places say the publish_post is deprecated and others that say to ignore that it still works. I’m currently using wordpress version 4.2.4 and here is the code I put in my functions.

    function gh_post_published_create_topic( $ID, $post ) {
        $checkduplicate = get_post_meta( $ID, 'gh_topic_created',true);
        if ( empty( $checkduplicate ) ) {
            $author = $post->post_author;
            $name = get_the_author_meta( 'display_name', $author );
            $title = $post->post_title;
            $permalink = get_permalink( $ID );
            $excerpt = $post->post_excerpt;
            $subject = sprintf( '%s: %s', $name, $title );
            $message = sprintf ('[b]%s[/b]' . "\n" . 'By %s' . "\n\n" . '[i]%s[/i]' . "\n\n" . 'View: %s' . "\n\n", $title, $name, $excerpt, $permalink );
            $tags = wp_get_post_tags( $ID, array( 'fields' => 'names' ) );
    
            $new_post = array(
                'post_title'    => $subject,
                'post_content'  => $message,
                'post_parent' => 72,
                'post_status'   => 'publish',
                'post_type' => 'topic'
            );
            $topic_meta = array(
                'forum_id'       => $new_post['post_parent']
            );
            $topic_id = bbp_insert_topic($new_post, $topic_meta);
            if ( !empty( $topic_id ) ) {
                add_post_meta($ID, 'gh_topic_created', 'true', true);
                $term_taxonomy_ids = wp_set_object_terms( $topic_id, $tags, 'topic-tag' );
                bbp_update_forum(array('forum_id' => 72));
            }
        }
    }
    add_action( 'publish_post', 'post_published_create_topic', 10, 2 );

    Any help on why this wouldn’t work for me would me much appreciated.


    Robkk
    Moderator

    @robkk

    yeah I can’t get it to work either.

    I think you could also just use the plugin bbPress Topics for posts for this kind of functionality though.


    ghoush
    Participant

    @ghoush

    Are you getting any error messages in your error logs? This code, unmodified, is working fine for me on 4.2.4.


    Leng7881
    Participant

    @leng7881

    I’m getting this error:

    PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘post_published_create_topic’ not found or invalid function name in C:\xampp\htdocs\alpa\wp-includes\plugin.php on line 496


    Leng7881
    Participant

    @leng7881

    Log’s who knew? From further examination due to the log telling me to look I found that I have a gh_ in front of the function name when declaring it. There error is also in your updated code above. Now that I took that out it works great. Here is the code without the minor syntax error and thank you for this.

    function post_published_create_topic( $ID, $post ) {
        $checkduplicate = get_post_meta( $ID, 'gh_topic_created',true);
        if ( empty( $checkduplicate ) ) {
            $author = $post->post_author;
            $name = get_the_author_meta( 'display_name', $author );
            $title = $post->post_title;
            $permalink = get_permalink( $ID );
            $excerpt = $post->post_excerpt;
            $subject = sprintf( '%s: %s', $name, $title );
            $message = sprintf ('[b]%s[/b]' . "\n" . 'By %s' . "\n\n" . '[i]%s[/i]' . "\n\n" . 'View: %s' . "\n\n", $title, $name, $excerpt, $permalink );
            $tags = wp_get_post_tags( $ID, array( 'fields' => 'names' ) );
    
            $new_post = array(
                'post_title'    => $subject,
                'post_content'  => $message,
                'post_parent' => 72,
                'post_status'   => 'publish',
                'post_type' => 'topic'
            );
            $topic_meta = array(
                'forum_id'       => $new_post['post_parent']
            );
            $topic_id = bbp_insert_topic($new_post, $topic_meta);
            if ( !empty( $topic_id ) ) {
                add_post_meta($ID, 'gh_topic_created', 'true', true);
                $term_taxonomy_ids = wp_set_object_terms( $topic_id, $tags, 'topic-tag' );
                bbp_update_forum(array('forum_id' => 72));
            }
        }
    }
    add_action( 'publish_post', 'post_published_create_topic', 10, 2 );

    Robkk
    Moderator

    @robkk

    @leng7881’s code worked for me @ghoush


    ghoush
    Participant

    @ghoush

    Yep, I did that. Nice. Thanks for posting that fix. I thought I had edited that out.

    I can’t edit my earlier posts it seems, they are too old, could a mod maybe make that fix for us?

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar