doukuang8166 2018-12-22 16:36
浏览 99
已采纳

如何将菜单项及其内容限制为Woocommerce中的已登录用户

On WordPress, I'm setting up my online shop with Woocommerce plugin. I have a PHP script that generate a pricing table.

But actually everyone can see it.

How can I add it to my WordPress menu and none could see it without being login in?

  • 写回答

1条回答 默认 最新

  • dongzongpeng6474 2018-12-22 16:43
    关注

    In your script you will add is_user_logged_in() in an IF statement like:

    function my_php_script_table(){
        if( is_user_logged_in() ) {
            // The function code
        } else {
            return; // Exit
        }
    } 
    

    Now in your Wordpress menu, you can add additional CSS classes enabling "css Classes" in the "Screen options" tab located up right of the "Menus" settings page.

    enter image description here

    Then once enabled, in your menu item you will be able to set a custom CSS class:

    enter image description here

    Then you will be able to hide from unlogged users this menu item using (here the additional class is hide_if_non_logged):

    add_action( 'wp_head', 'hide_if_non_logged_custom_inline_css', 500 );
    function hide_if_non_logged_custom_inline_css() {
        if( ! is_user_logged_in() ) {
            ?><style>li.hide_if_non_logged { display: none !important;}</style><?php
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    You can even add redirection when user is not logged in for the page where the content that you want to hide from non logged users (You will need to set the correct page slug or ID in the code):

    add_action( 'template_redirect', 'custom_unlogged_user_redirection' );
    function custom_unlogged_user_redirection() {
        // Below set your correct page slug
        if ( ! is_user_logged_in() && is_page( 'pricing-table' ) ){
            // Redirect to logout URL
            wp_safe_redirect( get_home_url() );
            exit(); // Always exit
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现