我正在创建我的第一个WordPress插件。这是一个扩展Visual Composer的简单插件,允许您删除VC中的元素选项(如屏幕截图所示)。
在插件“选项”页面上,我正在创建一个表单来显示/隐藏元素。这是我的代码:
// Going to add a conditional statement here to run this vc_remove_element function if the checkbox for Row is selected
vc_remove_element( "vc_row" ); // Row
add_action(\'admin_menu\', \'plugin_admin_settings_tab\');
function plugin_admin_settings_tab() {
add_options_page(\'Remove Visual Composer Elements\', \'Remove Visual Composer Elements\', \'manage_options\', \'remove_visual_composer_elements\', \'plugin_rvce_options_page\');
}
?>
<?php function plugin_rvce_options_page() { ?>
<div>
<form action="options.php" method="post">
<?php settings_fields(\'plugin_options\'); ?>
<?php do_settings_sections(\'plugin\'); ?>
<input name="Submit" type="submit" value="<?php esc_attr_e(\'Save Changes\'); ?>" />
</form>
</div>
<?php } ?>
<?php
// ADMIN SETTINGS
add_action(\'admin_init\', \'plugin_rvce_admin_init\');
function plugin_rvce_admin_init(){
register_setting( \'plugin_options\', \'plugin_options\', \'plugin_rvce_options_validate\' );
add_settings_section(\'plugin_main\', \'Visual Composer Element Settings\', \'plugin_rvce_section_text\', \'plugin\');
add_settings_field(\'Checkbox Element\', \'Row\', \'sandbox_checkbox_element_callback\', \'plugin\', \'plugin_main\' );
}
function sandbox_checkbox_element_callback() {
$options = get_option( \'plugin_options\' );
$html = \'<input type="checkbox" id="checkbox_example" name="sandbox_theme_input_examples[checkbox_example]" value="1"\' . checked( 1, $options[\'checkbox_example\'], false ) . \'/>\';
$html .= \'<label for="checkbox_example"> Hide</label>\';
echo $html;
}
?>
<?php function plugin_rvce_section_text() {
echo \'<p>Remove Visual Composer elements from the interface.</p>\';
} ?>
<?php // validate our options
function plugin_rvce_options_validate($input) {
$options = get_option(\'plugin_options\');
return $options;
}
表单在前端看起来是正确的,但当我单击复选框并保存它时。。未保存设置。我可以看出,sandbox\\u checkbox\\u element\\u回调函数没有正确设置,但我无法找到正确的设置方法。
形式:
有人能帮我做到这一点吗?
SO网友:dingo_d
这应该可以做到:
// Going to add a conditional statement here to run this vc_remove_element function if the checkbox for Row is selected
vc_remove_element( "vc_row" ); // Row
add_action(\'admin_menu\', \'plugin_admin_settings_tab\');
function plugin_admin_settings_tab() {
add_options_page(\'Remove Visual Composer Elements\', \'Remove Visual Composer Elements\', \'manage_options\', \'remove_visual_composer_elements\', \'plugin_rvce_options_page\');
add_action(\'admin_init\', \'plugin_rvce_admin_init\');
}
?>
<?php function plugin_rvce_options_page() { ?>
<div>
<form action="options.php" method="post">
<?php settings_fields(\'plugin_options\'); ?>
<?php do_settings_sections(\'plugin\'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
<?php
// ADMIN SETTINGS
function plugin_rvce_admin_init(){
register_setting( \'plugin_options\', \'plugin_options\' );
add_settings_section(\'plugin_main\', \'Visual Composer Element Settings\', \'plugin_rvce_section_text\', \'plugin\');
add_settings_field(\'Checkbox Element\', \'Row\', \'sandbox_checkbox_element_callback\', \'plugin\', \'plugin_main\' );
}
function sandbox_checkbox_element_callback() {
$options = get_option( \'plugin_options\' );
$checked = ( isset($options[\'checkbox_example\']) && $options[\'checkbox_example\'] == 1) ? 1 : 0;
$html = \'<input type="checkbox" id="checkbox_example" name="plugin_options[checkbox_example]" value="1"\' . checked( 1, $checked, false ) . \'/>\';
$html .= \'<label for="checkbox_example"> Hide</label>\';
echo $html;
}
?>
<?php function plugin_rvce_section_text() {
echo \'<p>Remove Visual Composer elements from the interface.</p>\';
} ?>
<?php // validate our options
function plugin_rvce_options_validate($input) {
$options = get_option(\'plugin_options\');
return $options;
}
您需要首先获得选中的值,然后查看它是否设置在第一位。因为当您取消选中复选框时
plugin_options
在数据库中的
wp_options
桌子什么都不是。因此,如果没有什么,则需要设置
$checked
为0,否则为1。此外,输入字段的名称错误,我将提交按钮替换为默认按钮。
测试了216个主题,对我有用:)希望这有帮助。