• Basically I’d like a post created automatically when a new user registers. The new user should be tied to the post as its author. I have a form builder plug installed that allows me to collect additional info, one field being “Restaurant Name.” I’ve looked at my DB in phpMyAdmin and confirmed restaurant_admin living in user_meta.

    I’m still a coding baby, but I feel like I’m on the right track here. I tosesd the following (plus the applicable php open/close tags) into a file and installed and activated it like a plugin, but it’s not creating the post. Can anyone tell me the glaring mistakes I’m sure I’m making here?

    add_action( 'user_register', 'restaurant_profile_create', 10, 1 );
    function restaurant_profile_create( $user_id )
    {
        // Get user info
        $user_meta = get_userdata( $user_id );
    
        // Create a new post
        $user_post = array(
            'post_title'   => $user_meta->restaurant_name,
            'post_type'    => 'post', 
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    
    }
    • This topic was modified 7 years, 5 months ago by justbishop.
    • This topic was modified 7 years, 5 months ago by justbishop.
Viewing 5 replies - 16 through 20 (of 20 total)
  • Moderator bcworkz

    (@bcworkz)

    I hate it when that happens! You are not the first nor will be the last to be stymied by spurious, forgotten code ??

    If your plugin is still not inserting posts, the ‘user_register’ action is either not firing or has been hijacked by other code. The usual cause is a plugin or theme conflict. Please try deactivating all plugins but yours and switching to one of the twenty* default themes. Registrations should then cause posts to be inserted. Begin restoring your normal configuration, one module at a time, testing after each. When the posts stop being inserted again, the last activated module is the culprit.

    Either find a viable alternative or get with the author and attempt to get the conflict resolved.

    BTW, your plugin is missing this line:
    'post_content' => print_r( $user_meta, true ),

    Since no post is inserted, it doesn’t matter. Should a post be inserted, it tells us how to deal with $user_meta.

    Another thing, if you haven’t already done so, is to define WP_DEBUG as true in wp-config.php. Any error messages that appear should be resolved, even if they are minor and of little consequence.

    Thread Starter justbishop

    (@justbishop)

    Thanks for the reply ??

    Actually it was debug mode that alerted me to the rogue code in a template file. That was literally the only line generated as I clicked around, so I don’t think there should be anything else lurking about.

    I fear it’s the Pie Register plugin that may be “hijacking” user_register, which will need to be resolved as I need to be able to charge a fee for registration, which is what I’m using Pie for in the first place. I did a bit of research and tried replacing user_register with one of the Pie actions that is apparently happening once it creates a new registered user, but that wasn’t generating the post I need, either.

    Next step is definitely the plugin deactivation/reactivation. Ughhhh.

    Moderator bcworkz

    (@bcworkz)

    It may be too late to comment further on deactivating plugins, but on the chance you’re procrastinating on this unenviable task… Deactivate the most suspect module first and test. If you find that solves the problem, there’s no need to go further, you’ve found the culprit.

    It may be the plugin isn’t so much hijacking as completely replacing the registration. If the plugin does turn out to be the problem, hopefully that plugin has its own hooks or pluggable function you can use to achieve the same end. If you need help specifically for this plugin, I recommend you use their dedicated support forum, as I’m not knowledgeable about this plugin.

    Even if there is no hook available, you could directly hack the plugin to get it to meet your needs. This is generally not recommended because your hack will be overwritten after every update, so you would need to keep re-applying it. Far from ideal, but some find it an acceptable price to pay.

    If you cannot get any good answers regarding this plugin (and if it is the cause of your trouble), and if you’re using the free version, let me know, when I find some time I can try digging deeper. Maybe it fires another unrelated WP hook we can make use of. If it’s the pro version, I won’t be able to help because the source code is not accessible to me.

    Thread Starter justbishop

    (@justbishop)

    Ughhhhh…so via process of elimination, I’ve discovered the issue arises when I activate the “pay to join” feature of the Pie Register plugin. Something about that is hijacking and altering things so that no post is created when the user registers. Works fine when registration is free and the user isn’t redirected to Paypal.

    Continuing on their support forum to see if they have a better action hook thing to replace user_register

    Can’t thank you enough for your help!

    Moderator bcworkz

    (@bcworkz)

    You are welcome! I hope the path to a solution is an easy one, good luck.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘Auto-Generate Post on User Registration’ is closed to new replies.