Chức năng wordpress

Thêm đoạn code bên dưới vào file Functions. php

add_action( 'admin_menu', function () {
  // Bỏ Dashboard
  remove_menu_page( 'index.php' );
  // Bỏ Posts
  remove_menu_page( 'edit.php' );
  // Bỏ Posts -> Categories
  remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );
  // Bỏ Posts -> Tags
  remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );
  // Bỏ Media
  remove_menu_page( 'upload.php' );
  // Bỏ Media -> Library
  remove_submenu_page( 'upload.php', 'upload.php' );
  // Bỏ Media -> Add new media
  remove_submenu_page( 'upload.php', 'media-new.php' );
  // Bỏ Pages
  remove_menu_page( 'edit.php?post_type=page' );
  // Bỏ Pages -> All pages
  remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
  // Bỏ Pages -> Add new page
  remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
  // Bỏ Comments
  remove_menu_page( 'edit-comments.php' );
  // Bỏ Appearance
  remove_menu_page( 'themes.php' );
  // Bỏ Appearance -> Themes
  remove_submenu_page( 'themes.php', 'themes.php' );
  // Bỏ Appearance -> Customize
  remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );
  // Bỏ Appearance -> Widgets
  remove_submenu_page( 'themes.php', 'widgets.php' );
  // Bỏ Appearance -> Menus
  remove_submenu_page( 'themes.php', 'nav-menus.php.php' );
  // Bỏ Appearance -> Editor
  remove_submenu_page( 'themes.php', 'theme-editor.php' );
  // Bỏ Plugins
  remove_menu_page( 'plugins.php' );
  // Bỏ Plugins -> Installed plugins
  remove_submenu_page( 'plugins.php', 'plugins.php' );
  // Bỏ Plugins -> Add new plugins
  remove_submenu_page( 'plugins.php', 'plugin-install.php' );
  // Bỏ Plugins -> Plugin editor
  remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
  // Bỏ Users
  remove_menu_page( 'users.php' );
  // Bỏ Users -> Users
  remove_submenu_page( 'users.php', 'users.php' );
  // Bỏ Users -> New user
  remove_submenu_page( 'users.php', 'user-new.php' );
  // Bỏ Users -> Your profile
  remove_submenu_page( 'users.php', 'profile.php' );
  // Bỏ Tools
  remove_menu_page( 'tools.php' );
  // Bỏ Tools -> Available Tools
  remove_submenu_page( 'tools.php', 'tools.php' );
  // Bỏ Tools -> Import
  remove_submenu_page( 'tools.php', 'import.php' );
  // Bỏ Tools -> Export
  remove_submenu_page( 'tools.php', 'export.php' );
  // Bỏ Settings
  remove_menu_page( 'options-general.php' );
  // Bỏ Settings -> Writing
  remove_submenu_page( 'options-general.php', 'options-writing.php' );
  // Bỏ Settings -> Reading
  remove_submenu_page( 'options-general.php', 'options-reading.php' );
  // Bỏ Settings -> Discussion
  remove_submenu_page( 'options-general.php', 'options-discussion.php' );
  // Bỏ Settings -> Media
  remove_submenu_page( 'options-general.php', 'options-media.php' );
  // Bỏ Settings -> Permalinks
  remove_submenu_page( 'options-general.php', 'options-permalink.php' );
}, 999);

Đôi khi, bạn viết một chức năng và chỉ muốn thực hiện nó một lần duy nhất. Và trong bài viết này, mình sẽ hướng dẫn bạn cách đơn giản nhất để thực hiện. Start đầu hủy bỏ

Chức năng wordpress

Chức năng nào chỉ thực hiện một lần duy nhất?

Có rất nhiều vấn đề mà chúng ta chỉ cần chạy chức năng một lần duy nhất trong WordPress. Đặc biệt là khi bạn là một trình cắm hoặc chủ đề thành viên, bạn muốn thực hiện chức năng chỉ một lần khi chủ đề hoặc plugin của bạn được kích hoạt như

  • Tạo bảng mới trong cơ sở dữ liệu của WordPress
  • Chèn, hoặc cập nhật dữ liệu
  • Create new user
  • Xoa vai game nguoi dung
  • Khác

Thực hiện chức năng một lần

Nếu bạn là một thành viên WordPress lập trình, có thể bạn sẽ biết đến hàm get_option. Hàm này sẽ lấy giá trị của một tùy chọn và trả về sai nếu giá trị đó không tồn tại

<?php
function hk_run_code_one_time() {
    if ( !get_option('run_only_once') ):
 
        // Đặt code bạn muốn thực hiện ở đây
 
        add_option('run_only_once', 1); 
    endif;
}
add_action( 'init', 'hk_run_code_one_time' );

Ở đoạn mã trên, mình tận hưởng sử dụng hàm get_option() để kiểm tra xem tùy chọn run_only_once có tồn tại giá trị hay không. Nếu chưa thì sẽ thực thi mã này, sau đó sử dụng hàm add_option để gắn giá trị cho tùy chọn run_only_once

Sau khi giá trị của run_only_once được gắn, đoạn mã trên sẽ không bao giờ được thực thi lại nữa

Nếu bạn muốn chạy lại để kiểm tra, bạn có thể tìm và xóa tùy chọn run_only_once ở bảng wp_options trong cơ sở dữ liệu WordPress

Hoặc sử dụng hàm delete_option, cụ thể là delete_option('run_only_once') để loại bỏ tùy chọn

Lời kết

Mình hi vọng bài viết này sẽ giúp bạn viết được chức năng chỉ thực thi một lần duy nhất trong WordPress

Hãy để bình luận lại nếu bạn có bất kỳ câu hỏi bất kỳ. Nếu thấy bài viết giả hữu ích, bạn có thể theo dõi chuyên mục Thủ thuật WordPress và theo dõi Facebook để biết thêm nhiều kiến ​​thức mới nha