I'm currently trying to get my head around the settings api, which is been a bit of a struggle to be honest.
The problem I am having is that when I submit the form on the subpage, it just goes to the the options.php page?
Here is my code so far
function setup_theme_admin_menus() {
add_menu_page('Theme settings', 'SMate Options', 'manage_options',
'theme_settings', 'theme_settings_page');
add_submenu_page('theme_settings',
'Front Page Elements', 'Front Page', 'manage_options',
'theme_settings_fp', 'theme_front_page_settings');
add_submenu_page(
'theme_settings',
'Team Option',
'Team Option',
'manage_options',
'theme_team_options',
'theme_team_settings_fn'
);
}
add_action('admin_init', 'initialize_theme_options');
function initialize_theme_options(){
register_setting('team_details', 'team_details' );
add_settings_section(
'member1',
'MEMBER 1',
'theme_settings_fn',
'theme_team_options'
);
add_settings_field(
't1',
'Name',
'text_fn',
'theme_settings_team',
'member1'
);
add_settings_field(
'jt1',
'Job',
'text_fn',
'theme_settings_team',
'member1'
);
add_settings_field(
'lt1',
'Description',
'longtext_fn',
'theme_settings_team',
'member1'
);
}
add_action('admin_init', 'initialize_theme_options');
function theme_team_settings_fn() {
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>Sandbox Theme Options</h2>
<?php print_r('team_details');
settings_errors(); ?>
<form method="post" action="options.php">
<?php do_settings_sections('theme_team_options'); ?>
<?php submit_button(); ?>
</form>
</div><!-- /.wrap -->
<?php
}
Any help would be greatly appreciated as I seem to have hit a brick wall and all tutorials seem to go through using add_theme_page
For starters your register_settings('option_group','option_name); shouldn't be the same name I would do team_details_group and maybe team_details_options, etc.
Then do settings_fields('team_details_group') right inside the form after the
<form method=post action=options.php>
followed immediately by $options = get_option('team_details_options')
then use team_details_options to inside the input fields name attribute to save any data i.e:
<input type="text" name="team_details_options[memberName]" />
Let me know if this helps or you need more information.
Related
I have created a plugin and registered a new setting there.
class WordCountAndTimePlugin{
function __construct()
{
add_action('admin_menu', array($this, 'adminPage'));
add_action('admin_init', array($this, 'settings'));
}
function settings(){
add_settings_section('wcp_first_section', null, null, 'word-count-settings-page');
add_settings_field('wcp_headline', 'Headline Text', array(
$this, 'headlineHTML'
), 'word-count-settings-page', 'wcp_first_section');
register_setting('wordcountplugin', 'wcp_headline', array(
'sanitize_callback' => 'sanitize_text_field',
'default' => 'Post Statistics'
));
}
function headlineHTML(){?>
<input type="text" name="wcp_headline" value="<?php echo esc_attr(get_option('wcp_headline')) ?>">
<?php }
function adminPage(){
add_options_page('Word Count Settings', __('Word Count', 'wcpdomain'), 'manage_options', 'word-count-settings-page',
array($this, 'ourHTML'));
}
function ourHTML(){ ?>
<div class="wrap">
<h1>
Word Count Settings
</h1>
<form action="options.php" method="POST">
<?php
settings_fields('wordcountplugin');
do_settings_sections('word-count-settings-page');
submit_button();
?>
</form>
</div>
<?php }
}
$wordCountAndTimePlugin = new WordCountAndTimePlugin();
This plugin creates a setting in worpress menu.
And create a page to save this setting in a setting table.
I have added a custom page template for WordPress. And what I need is to access this wcp_headline value inside.
<?php
/* Template Name: My Template */
// need to call it here
?>
Is there a way to call this wcp_headline value inside this template?
You can use the get_option function like you used it in your plugin:
$wcp_headline = esc_attr(get_option('wcp_headline'));
The $wcp_headline variable can then be used anywhere in your theme file.
The only proper way I found is to use the ACF Pro plugin.
Is there any other FREE way how to do it?
Example use case: phone number or logo, which is present on all pages of the template, and should be editable in the admin panel.
You can create the custom settings with either using plugin or using theme. I created custom settings fields with plugin and you can use and put required code into theme also. I created simple input field but you can create any of the field.
<?php
/*
* Plugin Name: Register Custom Settings
* Description: Adds custom setting field in admin.
* Version: 1.0.0
* Author: Wordpress
* Author uri: https://wordpress.org
* Text Domain: register-custom-settings
*/
function my_admin_menu() {
add_menu_page(
__( 'Sample page', 'register-custom-settings' ),
__( 'Sample menu', 'register-custom-settings' ),
'manage_options',
'sample-page',
'my_admin_page_contents',
'dashicons-schedule',
3
);
}
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_page_contents() {
?>
<h1> <?php esc_html_e( 'Welcome to my custom admin page.', 'register-custom-settings' ); ?> </h1>
<form method="POST" action="options.php">
<?php
settings_fields( 'sample-page' );
do_settings_sections( 'sample-page' );
submit_button();
?>
</form>
<?php
}
add_action( 'admin_init', 'my_settings_init' );
function my_settings_init() {
add_settings_section(
'sample_page_setting_section',
__( 'Custom settings', 'register-custom-settings' ),
'my_setting_section_callback_function',
'sample-page'
);
add_settings_field(
'my_setting_field',
__( 'My custom setting field', 'register-custom-settings' ),
'my_setting_markup',
'sample-page',
'sample_page_setting_section'
);
register_setting( 'sample-page', 'my_setting_field' );
}
function my_setting_section_callback_function() {
echo '<p>Intro text for our settings section</p>';
}
function my_setting_markup() {
?>
<label for="my-input"><?php _e( 'My Input' ); ?></label>
<input type="text" id="my_setting_field" name="my_setting_field" value="<?php echo get_option( 'my_setting_field' ); ?>">
<?php
}
After that you can get custom setting field data with get_option( 'my_setting_field' );
If you wish to create fields for templates and all requires to store different values for each templates than you needs to create the custom meta with "PAGE" post type which will display and store data into all page post type.
Here is the link for create custom meta
I am writing a plugin making admin options pages using the combo "register_setting", "add_settings_section", "add_settings_field". I have a parent menu and 2 sub-menus. Making admin pages in Wordpress is pretty straight forward... it starts when you make the admin page. Here are my parameters for it. (this is done in OOP)
'parent_slug' => 'Tee_Off',
'page_title' => 'Tee-Off Reports',
'menu_title' => 'Tee-Off Reports',
'capability' => 'manage_options',
'menu_slug' => 'tee_offs_reports',
'callback' => 'path to my template file goes here'; //this is correct in my code
so remember my menu slug "tee_offs_reports" posted above is needed for the next step...
class ClubHouseReports
{
function register()
{
add_action( 'admin_init', array( $this, 'teeoff_report_sellector') );
}
function teeoff_report_sellector() {
register_setting( 'my_options_group', 'timer_month' );
register_setting( 'my_options_group', 'timer_day' );
register_setting( 'my_options_group', 'timer_year' );
add_settings_section( 'teeoff-club-options', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee_offs_reports');
add_settings_field( 'timer_month', 'Month', array( $this, 'teeoff_month'), 'tee_offs_reports', 'teeoff-club-options');
add_settings_field( 'timer_day', 'Day', array( $this, 'teeoff_day'), 'tee_offs_reports', 'teeoff-club-options');
add_settings_field( 'timer_year', 'Year', array( $this, 'teeoff_year'), 'tee_offs_reports', 'teeoff-club-options');
}
function teeoff_month_section() {
echo '<p>Reports</p>';
}
function teeoff_month() {
$timer_month = esc_attr( get_option( 'timer_month' ) );
echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';
}
function teeoff_day() {
$timer_day = esc_attr( get_option( 'timer_day' ) );
echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';
}
function teeoff_year() {
$timer_year = esc_attr( get_option( 'timer_year' ) );
echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_year.'" placeholder="Year" />';
}
}
This class above is being call from the root plugin file, just like the one that works. An instance is made then the method "register()" is called.
In my template file I do this.... just like the one that does work.
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php settings_fields( 'my_options_group' ); ?>
<?php do_settings_sections('teeoff-club-options'); ?>
<?php submit_button(); ?>
</form>
SO my problem is... except for where the values need to change when you repeat the same code, in this case like, you need to assign a new menu slug, a new name for the "register_setting", and making sure it's aiming at the right callback, which I assure you everything is set right... for some reason this wont render.
I get no errors, all I see is the submit button, and if I use it I get a "success" message, so everything is in scope... but nothing is rendering on the "tee_offs_reports" page except for the submit button... which makes sense since it's hard coded there.
All this to say... I went over my code dozens of time to look for the smallest/stupidiest syntax mistake, or anything why this admin area wont work, while the other which uses the exact same structure and logic, works 100%. Do you see anything wrong with this code? Don't mind the big space between code above, that only happened when I pasted it here.... Is there a restriction in wordpress that I don't know of yet? Why is this not working? While a blue print of it works just fine.
OK, now I'm really baffled... I got it to work, but I highly doubt I'm following the codex rules... or am I?????
here is the code that is working 100%
function teeoff_report_sellector() {
register_setting( 'my_options_group', 'timer_month' );
register_setting( 'my_options_group', 'timer_day' );
register_setting( 'my_options_group', 'timer_year' );
add_settings_section('tee-off-reports', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee-off-reports');
add_settings_field('timer_month', 'Month', array( $this, 'teeoff_month'), 'tee-off-reports', 'tee-off-reports');
add_settings_field('timer_day', 'Day', array( $this, 'teeoff_day'), 'tee-off-reports', 'tee-off-reports');
add_settings_field('timer_year', 'Year', array( $this, 'teeoff_year'), 'tee-off-reports', 'tee-off-reports');
}
function teeoff_month_section() {
echo '<p>Reports</p>';
}
function teeoff_month() {
$timer_month = esc_attr( get_option( 'timer_month' ) );
echo '<input type="date" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';
}
function teeoff_day() {
$timer_day = esc_attr( get_option( 'timer_day' ) );
echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';
}
function teeoff_year() {
$timer_year = esc_attr( get_option( 'timer_year' ) );
echo '<input type="text" class="regular-text" name="timer_year" value="'.$timer_year.'" placeholder="Year" />';
}
What I did is... instead of using the menu slug like the codex tells you to do, ...(and the first admin page I created works well that way) I replaced that by the "add_settings_section" ID... so if you look at the before last parameter for each "add_settings_field" they are now assigned the ID of the section, codex is supposed to be looking for the menu slug... I always thought anyways... again the first one I did, works awesome that way... so the 2 last parameters of the "add_settings_field" are the same. Weird! Nowhere is the page slug mention in the code above, yet this works 100% and does not break any other code.
Now I'm wondering... did I find a bug in the Settings API? that will be fixed later on, and my plugin is going to stop working? Or did I stumble on the solution by being desperate trying a whole bunch of stuff???
I don't get it... if there's a real WordPress guru out there that can explain this to me I would be very appreciative!
in my woocomere product page there is tabs of a product to display different information (description, etc). But i wish to be able to in a specific tab turn in a ligthbox popup that shows text or a image. Is there any plugin available in wordpress for it?
Please try below code in function.php to show tab content in light box. Might be you will have to customize more but basic structure i am sharing with you.
add_action( 'woocommerce_product_write_panel_tabs','outputTabTitle');
add_action( 'woocommerce_product_write_panels','outputTabEditContent');
add_filter( 'woocommerce_product_tabs','productTab');
function outputTabTitle ()
{
?>
<li class="custom_tab">
Additional Information
</li>
<?php
}
function outputTabEditContent ()
{
global $woocommerce, $post;
echo '<div id="custom-tab" class="panel woocommerce_options_panel">';
/************Please put you lightbox text and image here.
echo '</div>';
}
function productTab ( $tabs )
{
$tabs['custom-tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'outputTabEditContent'
);
return $tabs;
}
How can I add a (draggable) meta box to an options page for a plugin I created? Is this even possible? Because if I look in the docs I see that I can only add it to a 'post', 'page', 'link', or 'custom_post_type'.
Yes it's possible. The code in your previous question was correct but it misses something important or you haven't added that code to the question.
Here is a demo plugin that can help you get it working.
This Plugin demonstrates how you can build your own plugin pages using the WordPress provided draggable metaboxes, requires WordPress 2.7 version, supports WordPress 2.8 changed boxing layout engine
The basic code from the demo plugin is the following. Note that in the full exemple the side meta boxes are not working nor the two columns layout as it was written for WordPress 2.8 and current version is almost 5.0.
// Source code by Frank Bueltge at gist.github.com/bueltge/757903
class howto_metabox_plugin {
function howto_metabox_plugin() {
add_action('admin_menu', array($this, 'on_admin_menu'));
add_action('admin_post_save_howto_metaboxes_general', array($this, 'on_save_changes'));
}
function on_admin_menu() {
$this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', 'howto_metaboxes', array($this, 'on_show_page'));
add_action('load-'.$this->pagehook, array($this, 'on_load_page'));
}
function on_load_page() {
wp_enqueue_script('common');
wp_enqueue_script('wp-lists');
wp_enqueue_script('postbox');
add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array($this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array($this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
}
function on_show_page() {
//define some data can be given to each metabox during rendering
$data = array('My Data 1', 'My Data 2', 'Available Data 1');
?>
<div id="howto-metaboxes-general" class="wrap">
<?php screen_icon('options-general'); ?>
<h2>Metabox Showcase Plugin Page</h2>
<form action="admin-post.php" method="post">
<?php wp_nonce_field('howto-metaboxes-general'); ?>
<input type="hidden" name="action" value="save_howto_metaboxes_general" />
<div id="poststuff" class="metabox-holder">
<div id="side-info-column" class="inner-sidebar">
<?php do_meta_boxes($this->pagehook, 'side', $data); ?>
</div>
<div id="post-body" class="has-sidebar">
<div id="post-body-content" class="has-sidebar-content">
<?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
<?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
<p>
<input type="submit" value="Save Changes" class="button-primary" name="Submit"/>
</p>
</div>
</div>
<br class="clear"/>
</div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
// close postboxes that should be closed
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
// postboxes setup
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
});
//]]>
</script>
<?php
}
function on_save_changes() {
if ( !current_user_can('manage_options') )
wp_die( __('Cheatin’ uh?') );
check_admin_referer('howto-metaboxes-general');
//process here your on $_POST validation and / or option saving
//lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
wp_redirect($_POST['_wp_http_referer']);
}
function on_contentbox_2_content($data) {
sort($data);
?>
<p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
<?php
}
function on_contentbox_additional_1_content($data) {
?>
<p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
<p>You can have as much as needed box groups.</p>
<?php
}
}
$my_howto_metabox_plugin = new howto_metabox_plugin();