修改用户.php页面以创建所点击的页面/发布按钮

时间:2015-04-14 作者:Mark

我正在尝试在用户中创建一个新列。带有按钮的php(Wordpress用户页面)。

与用户关联的每个按钮都必须创建一个页面或帖子(基于我的编程选择)。

我对如何调用函数从类内部创建页面/帖子有一些问题。

下面是我正在使用的一些示例代码:

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\' );
我三天来一直在解决这个问题,我需要一只大手。

提前谢谢你。

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

使用wp-admin/admin.php 使用action 参数:

<a href="<?php echo esc_url( admin_url( "admin.php?action=wpse_184153_create_page&user_id=$user_id" ) ) ?>">Create Page</a>
然后关注事件:

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\' );
您可以根据需要为函数命名—需要注意的关键是挂钩如何匹配action 参数,带后缀admin_action_.

结束