Advertisement
rujing

Untitled

Feb 13th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. function demo_settings_page()
  2. {
  3.     add_settings_section("section", "Настройки страниц:", null, "demo");
  4.     add_settings_field("demo-checkbox", "Закрытить внутр. страницы", "pages_404", "demo", "section");  
  5.     register_setting("section", "demo-checkbox");
  6. }
  7.  
  8. function pages_404()
  9. {
  10.    ?>
  11.         <!-- Here we are comparing stored value with 1. Stored value is 1 if user checks the checkbox otherwise empty string. -->
  12.         <input type="checkbox" name="demo-checkbox" value="1" <?php checked(1, get_option('demo-checkbox'), true); ?> />
  13.    <?php
  14. }
  15.  
  16. add_action("admin_init", "demo_settings_page");
  17.  
  18. function custom_settings()
  19. {
  20.   ?>
  21.       <div class="wrap">
  22.          <h1>Дополнительные настройки</h1>
  23.  
  24.          <form method="post" action="options.php">
  25.             <?php
  26.                settings_fields("section");
  27.  
  28.                do_settings_sections("demo");
  29.                  
  30.                submit_button();
  31.             ?>
  32.          </form>
  33.       </div>
  34.    <?php
  35. }
  36.  
  37. function menu_item()
  38. {
  39.   add_submenu_page("options-general.php", "Дополнительно", "Дополнительно", "manage_options", "custom-settigs", "custom_settings");
  40. }
  41.  
  42. add_action("admin_menu", "menu_item");
  43.  
  44.  
  45. $option_404 = get_option('demo-checkbox');
  46.  
  47. if(isset($option_404) && $option_404 == 1)
  48. {
  49.     add_action('template_redirect', 'test_redirect');
  50.     function test_redirect()
  51.     {
  52.         global $page, $wp_query;
  53.    
  54.         if(!is_front_page())
  55.         {
  56.             $wp_query->set_404();
  57.             wp_redirect( '/', 404 );
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement