1

I am trying to create a new Column in users.php (Wordpress Users Page) with buttons.

Each button, associated with the user, has to create a page or a post (based on my programmatically selection).

I have some problem on how to call the function to create the page/post from inside the class.

Here is some example code, I am playing with:

class Create_Pages_On_User_Click {

    ..............
    ..............
    ..............

    public function actions() {

        add_filter('manage_users_columns',  array( $this, 'hn_create_associated_page_column'));
        add_action('manage_users_custom_column', array( $this, 'hn_create_custom_pages_content'), 10, 3);

    }

    // Add Create Page Column
    public function hn_create_associated_page_column($columns) {
        $columns['create_page_associated'] = 'Page Associated Action';
        return $columns;
    }

    public function hn_create_custom_pages_content($value, $column_name, $user_id) {
        $postID_link = get_user_meta( $user_id, 'wp_ure_posts_list', true );

        if ( 'create_page_associated' == $column_name ) {
            if ($postID_link != null) {
                return '<button type="submit" class="btn btn-btn-warning" disabled>Page Created</button>';
            } else {
                $my_personal_page_name = get_user_meta( $user_id, 'wp_s2member_custom_fields', true);
                $name_of_the_personal_page = @$my_personal_page_name['personal_page_name'];

                //return "<a class='button button-primary' id='submitCreatePages' style='margin:0.25em 0' href='{$_SERVER["REQUEST_URI"]}?create_page.php&userID={$user_id}&lb_name={$name_of_the_personal_page}'>Create Page</a>";

                // Call programmatically_create_post() on click and pass $postID_link and $name_of_the_personal_page
                return '<input id="submitCreatePages" type="submit" value="Create" class="btn btn-warning" name="action" />';   

            }
        }
        return $value;
    }

}

function programmatically_create_post() {

    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    // Setup the author, slug, and title for the post
    $author_id = 1;
    $slug = 'example-post';
    $title = 'My Example Post';

    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {

        // Set the post ID so that we know the post was created successfully
        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'closed',
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_name'         =>  $slug,
                'post_title'        =>  $title,
                'post_status'       =>  'publish',
                'post_type'         =>  'post'
            )
        );

        die();

    // Otherwise, we'll stop
    } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );

I am stuck on this problem since three days and I need a big hand.

Thank you in advance.

2

Use wp-admin/admin.php with an action parameter:

<a href="<?php echo esc_url( admin_url( "admin.php?action=wpse_184153_create_page&user_id=$user_id" ) ) ?>">Create Page</a>

And then hook onto the event:

function wpse_184153_create_page() {
    $redirect = admin_url( 'users.php' );

    if ( ! empty( $_GET['user_id'] ) && $user = get_userdata( ( int ) $_GET['user_id'] ) ) {
        // Create your page

        // Add any URL parameters you need for the return page
        $redirect = add_query_arg( 'created_page', $user->ID, $redirect );
    }

    // Send 'em back to users.php
    wp_redirect( $redirect );
    exit;
}

add_action( 'admin_action_wpse_184153_create_page', 'wpse_184153_create_page' );

You can name the function anything you want - the key thing to notice is how the hook matches the value of your action parameter, with the suffix admin_action_.

  • 1
    I do not have words to thank you! You saved my brain sanity, thank you very much mate! I owe you a beer! :) – Mark Apr 14 '15 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.