【问题标题】:Preventing editing of pages with certain IDs防止编辑具有特定 ID 的页面
【发布时间】:2022-01-21 12:48:15
【问题描述】:

我创建了这个插件来防止某些帖子被删除。

此插件从页面列表中删除了 TRASH 选项。

register_activation_hook( __FILE__, 'prevent_page_delete_activate' );
function prevent_page_delete_activate() {
    add_action('admin_menu', 'prevent_page_delete');
}

add_filter ('user_has_cap', 'prevent_default_theme_deletion', 10, 3);

function prevent_default_theme_deletion($allcaps, $caps, $args) {
  $ids = [2000,2333,4444];
  // trash will ve removed for this page IDs

  $numElementos = count($ids);

  for ($i=0; $i < $numElementos; $i++) {

    $post_id = $ids[$i];
    if ( isset( $args[0] ) && isset( $args[2] ) && $args[2] == $post_id && $args[0] == 'delete_post' ) {
      $allcaps[ $caps[0] ] = false;
    }

  }
  return $allcaps;

}

从某些页面 ID 中删除 EDIT 链接的等价物是什么?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    根据您的最后评论,有几点需要考虑:

    • 以可视方式隐藏任何编辑/删除等提示。
    • 删除所有默认链接样式。
    • 防止直接访问。
    • 阻止数据库更新。

    您可以通过pre_post_update 操作钩子拦截帖子编辑,该钩子在现有帖子在数据库中更新之前立即触发。

    我们可以使用post_row_actions 过滤帖子列表中的行操作链接数组,以防止编辑操作。

    最后我们使用admin_head-{$hook_suffix}钩子来移除任何视觉风格并防止通过URL直接访问。

    所有内容都包含在一个类中以使其更容易。

    <?php
    
    if ( ! class_exists( 'wpso70412723' ) ) {
    
        class wpso70412723 {
    
            public $protected_posts_IDs = [ //Define the protected posts IDs
                2000,
                2333,
                4444,
            ];
    
            public function __construct() {
                add_action( 'pre_post_update', array( $this, 'wpso_70412723_prevent_database_update_on_specific_post_edit' ), 10, 2 );
                add_filter( 'post_row_actions', array( $this, 'wpso_70412723_remove_edit_related_actions_from_post_action_row' ), 10, 2 );
                add_action( 'admin_head-edit.php', array( $this, 'wpso_70412723_prevent_link_style_and_click_ability_from_post_title' ));
                add_action( 'admin_head-post.php', array( $this, 'wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL' ));
            } //public function __construct() {
    
            /**
             * Prevent specific posts edits actions.
             * Any post modifications (edit, delete, etc.) will be prevented.
             *
             * @param Integer $post_ID
             * @param Array $data
             */
            public function wpso_70412723_prevent_database_update_on_specific_post_edit($post_ID, $data) {
                if (in_array($post_ID, $this->protected_posts_IDs))
                    wp_die('You are not allowed to edit this post.', 'Something went wrong...', [
                        'back_link' => true
                    ]);
            } //public function wpso_70412723_prevent_database_update_on_specific_post_edit() {
    
            /**
             * Filters-out edit related actions from the array of row action links on the Posts list table.
             * 
             * @param String $actions An array of row action links.
             * @param Object (WP_Post) The post object.
             */
            public function wpso_70412723_remove_edit_related_actions_from_post_action_row($actions, $post) {
                if (in_array($post->ID, $this->protected_posts_IDs)) {
                    unset( $actions['edit'] );
                    unset( $actions['inline hide-if-no-js'] );
                    unset( $actions['trash'] );
                };
                return $actions;
            } //public function wpso_70412723_prevent_database_update_on_specific_post_edit() {
    
            /**
             * Prevent link style and click ability from the post title.
             * Fires in head section for a specific admin page.
             * In our case, the admin posts listing edit page.
             * 
             * @see https://developer.wordpress.org/reference/hooks/admin_head-hook_suffix/
             */
            public function wpso_70412723_prevent_link_style_and_click_ability_from_post_title() {
                if ( 'edit' !== get_current_screen()->base )
                    return;
    
                global $wp_query;
                $posts = $wp_query->posts;
                foreach ($posts as $post) {
                    if (in_array($post->ID, $this->protected_posts_IDs)) {
                        echo '<style type="text/css">
                            #the-list .post-' . $post->ID . ' strong a {
                                pointer-events: none;
                                color: initial;
                                text-decoration: none;
                            }
                        </style>';
                    };
                };
            } //public function wpso_70412723_prevent_link_style_and_click_ability_from_post_title() {
    
            /**
             * Prevent direct access to a specific post through URL.
             * Fires in head section for a specific admin page.
             * In our case, the admin posts listing edit page.
             * 
             * @see https://developer.wordpress.org/reference/hooks/admin_head-hook_suffix/
             */
            public function wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL() {
                if ( 'post' !== get_current_screen()->base )
                    return;
    
                if (in_array(get_the_ID(), $this->protected_posts_IDs)) {
                    wp_die('You are not allowed to edit this post.', 'Something went wrong...', [
                        'back_link' => true
                    ]);
                };
            } //public function wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL() {
    
        }; //class wpso70412723 {
    
        new wpso70412723();
    
    }; //if ( ! class_exists( 'wpso70412723' ) ) {
    

    顺便说一句,删除帖子被视为编辑。

    您不需要 prevent_default_theme_deletion() 函数。

    您可能很想使用 edit_post 操作挂钩,但这不起作用,因为:

    edit_post:在现有帖子更新后触发。

    “once”语句是我们需要使用pre_post_update 的原因。

    pre_post_update:在数据库中更新现有帖子之前立即触发。

    【讨论】:

    • 谢谢,但它不适合我。点击 EDIT 仍然允许用户编辑被禁止的帖子。最好让这些帖子的 EDIT 链接消失。
    • @Duck 确实,但这不是你问的。他们可以单击按钮并提交,但不会注册编辑。数据库不会接受编辑。您是否也想阻止向用户显示编辑按钮和页面?
    • 是的,我不想显示编辑按钮,就像我在我发布的代码上的垃圾桶按钮一样。让用户编辑保存但不注册到数据库会出现bug。
    • @Duck 我已经编辑了答案,它应该按照你的描述做,让我知道。
    • @JohnDoe 我已经编辑了答案,它应该按照你的描述做,让我知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2012-10-08
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多