我现在在一个类中组合了几个函数。这样做的原因是所有这些函数都朝着一个目标前进,而在这些函数之间使用全局变量更合适。该类由三个主要部分组成,第一部分添加我的元框,第二部分添加创建边栏的选项页面,第三部分实际注册边栏。
我现在的问题是,第一节和第二节只需要在管理端运行。我知道如何在正常功能上实现这一点。我的问题是如何在课堂上做到这一点。我知道您可以将整个类包装在is_admin()
标记,但这在这里不起作用,因为我的边栏不会显示在前端。
我如何才能在is_admin()
标签这是我的课
class PieterGoosen_Custom_Sidebars {
private $sdwas;
public function __construct() {
add_action( \'add_meta_boxes\', array( $this, \'add_cspp\' ) );
add_action( \'save_post\', array( $this, \'save_cspp\' ) );
add_action( \'admin_init\', array( $this, \'init\' ) );
add_action( \'admin_menu\', array( $this, \'add_page\' ) );
add_action( \'widgets_init\', array( $this, \'sidebars_register\' ) );
}
/*----------------------------------------------------------------------------------
*
* Create and add meta box to page edit screen
*
*---------------------------------------------------------------------------------*/
public function add_cspp() {
//Meta box in page screen
add_meta_box(
\'custom_sidebar_per_page\',
__( \'Sidebar options\', \'pietergoosen\' ),
array( $this, \'cspp_link\' ),
\'page\',
\'side\',
\'high\'
);
// Meta box in post screen
add_meta_box(
\'custom_sidebar_per_page\',
__( \'Sidebar options\', \'pietergoosen\' ),
array( $this, \'cspp_link\' ),
\'post\',
\'side\',
\'high\'
);
}
public function cspp_link( $post ) {
wp_nonce_field( \'cspp_meta_nonce\', \'cspp_nonce\' );
$custom = get_post_custom($post->ID);
if(isset($custom[\'_custom_sidebar_per_page\']))
$val = $custom[\'_custom_sidebar_per_page\'][0];
else
$val = "default";
// The actual fields for data entry
$output = \'<p><label for="pietergoosen_new_field">\'.__( \'Choose a sidebar to display\', \'pietergoosen\' ).\'</label></p>\';
$output .= \'<select name="custom_sidebar_per_page">\';
// Add a default option
$output .= \'<option\';
if($val == "default")
$output .= \' selected="selected"\';
$output .= \' value="default">\'.__( \'No Specified Sidebar\', \'pietergoosen\' ).\'</option>\';
// Fill the select element with all registered sidebars
if(!empty($this->sdwas))
foreach($this->sdwas as $sid => $sdwa) {
$output .= \'<option\';
if($sdwa == $val)
$output .= \' selected="selected"\';
$output .= \' value="\'.$sdwa.\'">\'.$sdwa.\'</option>\';
}
$output .= \'</select>\';
echo $output;
}
public function save_cspp($post_id){
// Check if our nonce is set.
$nonce = filter_input(INPUT_POST, \'cspp_nonce\', FILTER_SANITIZE_STRING);
// Verify that the nonce is valid.
if ( empty($nonce) || ! wp_verify_nonce( $nonce, \'cspp_meta_nonce\' ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check the user\'s permissions for pages.
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
// Check the user\'s permissions for posts.
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
/* OK, its safe for us to save the data now. */
$custom = filter_input(INPUT_POST, \'custom_sidebar_per_page\', FILTER_SANITIZE_STRING);
if(!empty( $_POST[\'custom_sidebar_per_page\'] )) {
update_post_meta($post_id, \'_custom_sidebar_per_page\', $custom);
}else{
delete_post_meta( $post_id, \'_custom_sidebar_per_page\');
}
}
/*----------------------------------------------------------------------------------
*
* Register and add a options screen under the "Appearance" menu screen.
* Sidebars are created/deleted here.
*
*---------------------------------------------------------------------------------*/
public function init() {
register_setting(
\'set_fields\',
\'cspp_get_option\',
array( $this, \'sanitation\' )
);
add_settings_section(
\'general\',
__( \'Custom sidebars per page\', \'pietergoosen\' ),
\'__return_false\',
\'theme_options\'
);
add_settings_field(
\'custom_sidebar_per_page\',
__( \'Custom sidebars\', \'pietergoosen\' ),
array( $this, \'settings_field\' ),
\'theme_options\',
\'general\'
);
}
public function add_page() {
add_theme_page(
__( \'Custom Sidebars\', \'pietergoosen\' ),
__( \'Custom Sidebars\', \'pietergoosen\' ),
\'edit_theme_options\',
\'theme_options\',
array( $this, \'render_page\' )
);
}
public function render_page() {
if (! current_user_can(\'edit_theme_options\') )
return;
?>
<div class="wrap">
<h2><?php _e( \'Custom Sidebars Per Page Options\', \'pietergoosen\' ); ?></h2>
<p>
<?php
_e( \'Create or remove custom sidebars that can be individually used on posts and pages\', \'pietergoosen\' )
?>
</p>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( \'set_fields\' );
do_settings_sections( \'theme_options\' );
submit_button();
?>
</form>
</div>
<?php
}
public function sanitation( $input ) {
$output = array();
if ( isset( $input[\'_custom_sidebar_per_page\'] ) ) {
$output[\'_custom_sidebar_per_page\'] = $input[\'_custom_sidebar_per_page\'];
}
return $output;
}
public function settings_field() {
$option1 = get_option( \'cspp_get_option\' );
// A bit of jQuery to handle interactions (add / remove sidebars)
$val1 = \'<script type="text/javascript">\';
$val1 .= \'
var $ = jQuery;
$(document).ready(function(){
$(".sidebar_management").on("click", ".delete", function(){
$(this).parent().remove();
});
$("#add_sidebar").click(function(){
$(".sidebar_management ul").append("<li>"+$("#new_sidebar_name").val()+" <a href=\\\'#\\\' class=\\\'delete\\\'>\'.__(\'Delete\', \'pietergoosen\').\'</a> <input type=\\\'hidden\\\' name=\\\'cspp_get_option[_custom_sidebar_per_page][]\\\' value=\\\'"+$("#new_sidebar_name").val()+"\\\' /></li>");
$("#new_sidebar_name").val("");
})
})
\';
$val1 .= \'</script>\';
$val1 .= \'<div class="sidebar_management">\';
$val1 .= \'<p><input type="text" id="new_sidebar_name" /></p><p><input class="button-primary" type="button" id="add_sidebar" value="\'.__(\'Add\', \'pietergoosen\').\'" /></p>\';
$val1 .= \'<ul>\';
// Display all custom sidebars
if(isset($option1[\'_custom_sidebar_per_page\']) ? esc_attr($option1[\'_custom_sidebar_per_page\']) : \'\') {
$csppval = $option1[\'_custom_sidebar_per_page\'];
$i = 0;
foreach($csppval as $sdwa) {
$val1 .= \'<li>\'.$sdwa.\' <a href="#" class="delete">\'.__( \'Delete\', \'pietergoosen\' ).\'</a><input type="hidden" name="cspp_get_option[_custom_sidebar_per_page][]" value="\'.$sdwa.\'" /></li>\';
$i++;
}
}
$val1 .= \'</ul>\';
$val1 .= \'</div>\';
echo $val1;
}
/*----------------------------------------------------------------------------------
*
* Register the custom sidebars if a value is entered in save in the options
* screens. Also create a sidebar ID from this value
*
*---------------------------------------------------------------------------------*/
public function sidebars_register() {
$option2 = get_option( \'cspp_get_option\' );
if( isset($option2[\'_custom_sidebar_per_page\']))
$this->sdwas = $option2[\'_custom_sidebar_per_page\'];
if( isset($this->sdwas) && sizeof($this->sdwas) > 0) {
foreach($this->sdwas as $sid => $sdwa) {
register_sidebar(
array (
\'name\' => $sdwa,
\'id\' => sanitize_title($sdwa),
\'description\' => __( \'Page specific sidebars that can be chosen per page\', \'pietergoosen\' ),
\'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</aside>\',
\'before_title\' => \'<h1 class="widget-title">\',
\'after_title\' => \'</h1>\',
)
);
}
}
}
}
$pgsidebar = new PieterGoosen_Custom_Sidebars();
EDIT
正如@isNaN所建议的,我已经试过了。
public function __construct() {
if (is_admin()) {
add_action( \'add_meta_boxes\', array( $this, \'add_cspp\' ) );
add_action( \'save_post\', array( $this, \'save_cspp\' ) );
add_action( \'admin_init\', array( $this, \'init\' ) );
add_action( \'admin_menu\', array( $this, \'add_page\' ) );
}
add_action( \'widgets_init\', array( $this, \'sidebars_register\' ) );
}
我不确定这是不是正确的方法。所有函数仍使用此方法执行。只是钩子没有执行,因为它们在条件标记中。我真的认为应该有一种方法可以在
is_admin()
条件标记,而不仅仅是操作挂钩