Support » Developing with WordPress » Automated Creation of a Page+Post+Category+Tag

  • ante1974

    (@ante1974)


    Hi folks,

    I promise there is a question at the end of this!

    I’m developing a site where at the moment there are a number of manual steps for a Administrator.

    The first is to create a profile page. This page is automatically populated with content by entering a User ID into a custom field and the title is based on an authors first name and last name. The page also needs to be “parented” by the letter that corresponds to the index letter of the user’s first name.

    Further manual steps of creating a Category and Tag based on the user’s first and last names are also required.

    I had a look at the code below and considered using this as the basis for extending my custom registration Page Template, in order to automatically create a Page, Post, Category and Tag, all based on a user’s first and last names. However, it crossed my mind that not all users will be approved, so I could be creating an automated process that might cause a headache for the Administrator, who then might have to remove any “spammy” Users, along with the associated Pages, Posts, Categories and Tags of any such users.

    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
     
    function myplugin_registration_save( $user_id ) {
     
        if ( isset( $_POST['first_name'] ) )
            update_user_meta($user_id, 'first_name', $_POST['first_name']);
        
        // Code to create page, post, category and tag based on first and last name
     
    }

    From: https://developer.wordpress.org/reference/hooks/user_register/

    So my question what is the most “portable” way to trigger the creation of Pages, a single Post, Category and Tag once a user is Verified? By “portable” I mean, I don’t want to modify the core in a way that means I lose my changes when WordPress or a Theme updates, or if it is necessary to do so than minimal rework would be required to get those changes back in?

    Thanks,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Minimal rework? Don’t even consider it, it will not be worth it. There’s invariably a clean (no core mods) solution to every need. It may be more onerous to accomplish than we like, but it’ll be clean.

    There’s no approval process for new users on open registration sites. Once they set their password they’re in. What you could do though is assign new users a very restricted role. Admins could “approve” new registrations by upgrading their role. You could then auto-generate supporting data when the role is upgraded. It’s still a manual process, but at least all the elements are rolled into one action. The ‘set_user_role’ action can be used for this.

    The other approach would be to auto-generate all the data for any new user, but make the removal process easier. Use the ‘register_new_user’ action hook to add the data. Removing a user’s page when the associated user is removed is already fairly easy. You could hook into this process to also auto-remove any related taxonomy terms. This scheme could be stymied should a user alter their profile name so that the data used to create name taxonomy terms is no longer available. It’d be better if the user ID were used since it cannot be changed. Or save the initial name values in user meta for future reference regardless of current profile values. The hook to use to remove taxonomy terms with user removal is “delete_user”

    Thread Starter ante1974

    (@ante1974)

    @bcworkz Thanks for this. Just to clarify, the site isn’t open registration exactly, it requires approval from an admin before they can log in.

    I’ve been looking for a plugin for a while that might capture these bespoke needs, but in the end had to build a specific page template to cover registration, profile edit and profile viewing.

    In terms of looking at the removal process, is this less impacting on the core than looking at the activation hook?

    It just crossed my mind too, I currently have a plugin installed that shows user IO’s on the Users page (users.php), if I could link clicking on a user ID with a query to create tags and categories based on the user name, that could work too.

    Moderator bcworkz

    (@bcworkz)

    I’m unsure how registration approval is accomplished. It likely involves some hook firing off. If you could determine what hook that is and if appropriate user data is passed to the hook callback, then any other desired actions could be taken at that time.

    Similarly, any event on the user list table could in theory trigger additional actions for the user involved. For example, it’s possible to add extra action links (the ones that appear below any user on hover: Edit, Delete, etc.). That link could lead to custom code that creates the user page and taxonomy terms. It could even be made into an Ajax request instead so one needn’t leave the page in order to make things happen.

    Action link info and example code can be found at https://glennmessersmith.com/pages/custom_columns.html#rowact
    That’s the easy part. Making it into an Ajax request that does what you want will be the bulk of the effort.

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