在网站上,用户填写一张表格,在网站上创建他们的页面以显示信息.在My Account页面中,我创建了一个链接,将登录用户带到WordPress仪表板,他们可以在那里编辑此页面.

// Add Store link WooCommerce Navigation
add_filter ( 'woocommerce_account_menu_items', 'one_more_link' );
function one_more_link( $menu_links ){
    $new = array( 'stall-page' => 'Edit Stall Information' );

    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );

    return $menu_links;
}

// hook the URL
add_filter( 'woocommerce_get_endpoint_url', 'hook_endpoint', 10, 4 );
function hook_endpoint( $url, $endpoint, $value, $permalink ){
    if( 'stall-page' === $endpoint ) {
        $url = 'https://websitename.com/wp-admin/edit.php?post_type=stallholders';
    }
    return $url;
}

我想更进一步,直接链接到他们需要编辑的页面.

我一直在做实验,并取得了这样的成果:

// hook the URL
add_filter( 'woocommerce_get_endpoint_url', 'hook_endpoint', 10, 4 );
function hook_endpoint( $url, $endpoint, $value, $permalink ){
    $current_user = wp_get_current_user();
    $post_id = $current_user->post_ID;
    if( 'stall-page' === $endpoint ) {
        $url = 'https://websitename.com/wp-admin/post.php?post=' . $post_id . '&action=edit';
    }
    return $url;
}

但这是链接到用户ID而不是他们的自定义帖子类型页面吗?

推荐答案

您只需要查询用户的自定义帖子,并在URL中使用查询到的帖子ID.

try 以下替换代码:

// Add Store link WooCommerce Navigation
add_filter ( 'woocommerce_account_menu_items', 'one_more_link' );
function one_more_link( $menu_links ){
    $new = array( 'stall-page' => __('Edit Stall Information', 'woocommerce') );

    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );

    return $menu_links;
}

// Define 'stall-page' endpoint URL
add_filter( 'woocommerce_get_endpoint_url', 'define_stall_page_endpoint_url', 10, 4 );
function define_stall_page_endpoint_url( $endpoint_url, $endpoint, $value, $permalink ){
    global $current_user;

    // Get stallholders custom posts IDs from the user
    $post_ids = get_posts( array(
        'numberposts'   => 1,
        'post_author'   => $current_user->ID,
        'post_type'     => 'stallholders',
        'fields'        => 'ids',
    ));

    if( 'stall-page' === $endpoint && ! empty($post_ids) ) {
        return admin_url('post.php?post='.reset($post_ids).'&action=edit');
    }
    return $endpoint_url;
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.

Php相关问答推荐

如何在WordPress中为特定自定义帖子类型自定义URL struct

dockerized laravel服务器拒绝POST请求

从WooCommerce订单状态更改更新用户自定义元数据

Sylius php -如何将购物车传递到小枝模板(Sylius模板事件)

使用ESI的Symfony Sulu清漆

如何在Laravel Model中自定义多个日期属性的日期格式?

是否使用联系人表单7 wpcf7_Feedback_Response输出答复中的html?

在WooCommerce循环中添加子类别和产品之间的分隔符

Laravel withOnly不限制查询

$this->;db->;update();CodIgnitor中的功能不工作

Filament v3:具有 hasMany 和 BelongsTo 的关系管理器

如何在laravel中获得两个时间戳之间的小时和分钟差异

如何在 PHP 中以 hh:mm:ss 格式获取两个 strtotime datetime/s 之间的时间差

PHP Laravel 迁移文件不创建关系

注册命名空间后如何获取xml node 值?

如何在 Process facade 中转义特殊字符?

如何在光速网络服务器中使用 apache ..htaccess

使用 splat 运算符时按引用传递 (...)

fopen 功能不断向我发送我重新加载网页时已发布的重复版本的表单

PHP 从 API 获取随机项目