I'm having what im sure is a simple issue but i can't manage to figure it out.
I'm coding a theme options page for my wordpress template and i've managed to get it where the values are saved and i can use them on the site but whenever i reload the theme options page all the form fields are blank and any previously applied settings are gone. My code is below.
<?php
//Theme Options Functionality is Below
if (get_option('pardue_theme_options')){
$theme_options = get_option('pardue_theme_options');
} else {
add_option('pardue_theme_options', array(
'sidebar2_on' => true,
'footer_text' => 'Made by William'
));
}
?>
<?php add_action('admin_menu', 'theme_page_add');
function theme_page_add() {
add_submenu_page('themes.php', 'Pardue Theme Options', 'Theme Options', 'administrator', 'themeoptions', 'theme_page_options');
}
function theme_page_options() {
global $theme_options;
$new_values = array(
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('pardue_theme_options', $new_values);
$theme_options = get_option('pardue_theme_options');
echo '<div class="wrap">';
echo '<h2>Theme Options</h2>';
?>
<form action="themes.php?page=themeoptions" method="post">
<label for="footer_text">Footer Text: </label><input name="footer_text" id="footer_text" value="<?php echo $theme_options['footer_text']; ?>" /> <br /> <br />
<label for="sidebar_checkbox">Sidebar 2 on: </label> <input name="sidebar_checkbox" id="sidebar_checkbox" value="on" type="checkbox" /> <br /> <br />
<input type="submit" value="Update Options" name="submit" />
</form>
<?php
echo '</div>';
}
?>
I found a good solution for coding my theme options. The plugin at the link below makes it very easy to code up theme options.
Link to plugin
Documentation is included when you activate the plugin.
Related
I want to enable and disable custom meta-box on click of a button in my plugin. This button will be on the settings page of the plugin. This is what I've done so far:
function wpplugin_settings_page_markup()
{
if(!current_user_can('manage_options'))
{
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e(get_admin_page_title()); ?></h1>
<?php
?>
<form method="post" action="">
<input type="checkbox" id="postcb" name="postcheck" value="Post">
<label id='postcbid' name="labelpostcheck" for='postcb'>
Post
</label>
<input type="submit" value="submit" name="submit_btn">
</form>
</div>
<?php
function cd_meta_box_add()
{
$multi_posts=array('post', 'page');
foreach ($multi_posts as $multi_post) {
add_meta_box(
'my-meta-box-id', //id
'Custom Meta Box', //title
'cd_meta_box_cb', //callback
$multi_post, //post type
'normal', //position
'high' //priority
);
}
}
add_action('add_meta_boxes', 'cd_meta_box_add');
function cd_meta_box_cb()
{
echo'<b> This is Custom meta box </b>';
}
}
This code does not display the meta-box but I want the meta-box to be added only when that checkbox is checked, and removed when checkbox is not checked. Can anyone please help me achieve that? Thanks!
I read different articles, but they describe more other things. I do not want to make the wrong code with too much space. In the module there will be one single value that can be changed in the admin panel and other extraneous functions. How correctly to create a database for value of this window in a plugin?
I found the documentation, but I do not have enough experience to cut the excess, if it is.
My code /plugins/custom-counter/custom-counter.php
/*
Plugin Name: Custom Counter
Plugin URI: https://example.com
Description: This plugin adds counter.
Author: Kuznetsova Alexandra
Author URI: https://example.com
*/
// Hook for adding admin menus
add_action('admin_menu', 'custom_counter_menu');
// action function for above hook
function custom_counter_menu() {
// Add a submenu to Woocommerce menu:
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// custom_counter_page() displays the page content
function custom_counter_page() {
?>
<div class="wrap">
<h2>Custom Counter</h2>
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Total Counter</th>
<td><input type="text" name="custom-counter" value="..." /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
Updated - As this is a unique value that will be updated multiple times, you can use the wp_options table with the following WordPress dedicated functions:
get_option($option); where $option is the name of option to retrieve
update_option($option, $new_value); where $option is the name of option and $new_value the new value to be updated.
The fact that options are cached is not a problem if you use some tricks as you will see below, to avoid this data to be cached.
So try the following code:
// Add a custom admin submenu to Woocommerce
add_action('admin_menu', 'custom_counter_menu');
function custom_counter_menu() {
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// Content for the custom Woocommerce admin submenu
function custom_counter_page() {
$option_name = 'wc-custom-counter' ;
if( isset($_POST[$option_name]) ){
$new_value = sanitize_text_field( $_POST[$option_name] );
if ( get_option( $option_name ) !== false ) {
update_option($option_name, $new_value );
} else {
add_option( $option_name, $new_value, null, 'no' );
}
}
$default = ''; // Set the default value
$value = get_option( $option_name ) ? get_option( $option_name ) : $default;
?>
<div class="wrap">
<h2><?php _e('Custom Counter'); ?></h2>
<form method="post" action="">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Total Counter'); ?></th>
<td><input type="text" name="<?php echo $option_name; ?>" value="<?php echo $value; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
Code goes in function.php file of the active child theme (or active theme). tested and work.
If the option already exist, to avoid caching issue, you should need to delete it using:
delete_option('wc-custom-counter');
And adding it in your function.php file. Then browse any page of your web site and remove it.
If you only want to store one thing, like a setting, then you can use wp_options. You can then use WordPress functions to interact with the database instead of writing your own SQL.
I've added this code to my functions.php file:
function wpgood_nav_search( $items, $args ) {
$items .= '<li>' . get_search_form( false ) . '</li>';
return $items;
}
add_filter( 'wp_nav_menu_items','wpgood_nav_search', 10, 2 );
I've found it here:
adding search bar to the nav menu in wordpress
The problem: this code adds the search bar to desktop and mobile menus, but I need to add the search bar only to my mobile menu, because the desktop version of my site already has a search widget in the left sidebar.
How can I do this? Please, help me. Thanks!
Initializiation of my menus in functions.php :
// Initialize navigation menus
register_nav_menus(
array(
'primary-menu' => esc_html__( 'Primary Menu', 'bento' ),
'footer-menu' => esc_html__( 'Footer Menu', 'bento' ),
)
);
Thank you so much for your help! Finally I have decided to use my initial code and edit the searchbar's position and style with css, because I am not so good in coding:)
Basically do you do that by several process. Like following
By using jquery append function you can do. Paste this code in template JS file
$("here_put_menu_selector").append('
<li>
<form role="search" method="get" id="searchform" class="search-form" action="https://yourdomain.com/">
<div class="search-form-wrap">
<input type="text" value="" name="s" id="s" class="search-form-input" placeholder="search any"/>
<input type="submit" id="searchsubmit" class="button submit-button" value="Search"/>
</div>
</form>
</li>
');
Only on mobile devices visible that using CSS display property & hide that part from desktop/tablet devices.
Use the javascript code for adding the search box for mobile menu
$("ul.primary-mobile-menu').append('
// your html code to append
<li><form role="search" method="get" id="searchform" class="search-form" action="https://bookyspace.com/"><div class="search-form-wrap"> <input type="text" value="" name="s" id="s" class="search-form-input" placeholder="Знайти.."> <input type="submit" id="searchsubmit" class="button submit-button" value=""></div></form></li>
');
Thanks
#Vladislav Tiaglo
Add a class in the li item to the code you already used and said it´s working for both desktop and mobile:
<li> Add class to <li class="searchbox-position">
Then, in your css you code to hide the display for desktop:
#media (min-width: 840px) {
li.searchbox-position {
display: none !important;
}
}
Easiest way to make it work
I was using the code below as a Plugin to create a "Options Menu Page" for wordpress:
add_action('admin_init', 'cardin_options_init' );
add_action('admin_menu', 'cardin_options_add_page');
function cardin_options_init(){
register_setting( 'cardin_options_options', 'cardin_options');
}
function cardin_options_add_page() {
add_options_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
}
function cardin_options_do_page() {
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>Cardin Options</h2>
<form method="post" action="options.php">
<?php settings_fields('cardin_options_options'); ?>
<?php $options = get_option('cardin_options'); ?>
<table class="form-table">
<tr valign="top"><th scope="row">Information</th>
<td><input type="text" name="cardin_options[information]" value="<?php echo $options['information']; ?>" /></td>
</tr>
</table>
<input type="submit" class="button-primary" name="submit" value="<?php _e('Save Changes') ?>" />
</form>
</div>
<?php
}
It's working how it should, but, later I decided to create my Own Plugin Menu Page, not a 'Settings' sub-menu, then, I changed the line below:
add_options_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
to:
add_menu_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
It worked, but when i click "Save Changes" the "Settings Updated" message doesn't display anymore. What should I do to make it display again?
Thanks alot in advance and sorry for the bad english.
<?php if($_POST['oscimp_hidden'] == 'Y') { ?>
<div id="message" class="updated">
<p><strong><?php _e('Settings saved.') ?></strong></p>
</div>
edit:
heres a guide: http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/
search for: if($_POST['oscimp_hidden'] == 'Y') {
I would like to create a custom form for my WordPress blog which takes the user's email address and then adds it to a database. I know how to write the form and script to achieve the data storage. I don't know how I would go about sticking it on WordPress blog though.
Are there any plugins for this type of thing, or is there a way I can manually add the form to the page?
It's basically a signup for notifications box.
Thanks.
You can just add it using the text widget if your theme is widget ready
Look under appearances>widgets
You can add html to text widget
If you are using more than html you'll run into problems with the widget. Upon which I'd recommend that you create a widget yourself.
Here is code for a blank plugin. Add/call your code in the "widget" function.
<?php
/*
Plugin Name: Blank Plugin
Plugin URI: http://www.example.com/plugins/blankPlugin/
Description: This is a plugin template
Author: Your Name
Version: 0.1
Author URI: http://www.example.com/about/
*/
class blankPlugin extends WP_Widget {
function blankPlugin() { // The widget construct. Initiating our plugin data.
$widgetData = array( 'classname' => 'blankPlugin', 'description' => __( "A blank plugin widget" ) );
$this->WP_Widget('blankPlugin', __('Blank Plugin'), $widgetData);
}
function widget($args, $instance) { // Displays the widget on the screen.
extract($args);
echo $before_widget;
echo $before_title . $instance['title'] . $after_title;
echo 'The amount is: '.$instance['amount'];
echo $after_widget;
}
function update($new_instance, $old_instance) { // Updates the settings.
return $new_instance;
}
function form($instance) { // The admin form.
$defaults = array( 'title' => 'Wichita', 'amount' => '45' );
$instance = wp_parse_args($instance, $defaults); ?>
<div id="blankPlugin-admin-panel">
<p>
<label for="<?php echo $this->get_field_id("title"); ?>">Widget title:</label>
<input type="text" class="widefat" name="<?php echo $this->get_field_name("title"); ?>" id="<?php echo $this->get_field_id("title"); ?>" value="<?php echo $instance["title"]; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id("amount"); ?>">An amount:</label>
<input type="text" class="widefat" name="<?php echo $this->get_field_name("amount"); ?>" id="<?php echo $this->get_field_id("amount"); ?>" value="<?php echo $instance["amount"]; ?>" />
</p>
</div>
<?php }
}
// Register the widget.
add_action('widgets_init', create_function('', 'return register_widget("blankPlugin");'));
?>
For more information... (see links at the bottom of the page as well)
http://codex.wordpress.org/Widgets_API#Developing_Widgets
you can use this wordpress plugin http://wordpress.org/extend/plugins/contact-form-7/
or can do on your own base.
you can create a template inside your theme folder like this:-
<?php
/*Template Name: some thing*/
//your dynamic stuff here
?>
and can assign this template to your static page which you can create from the admin panel of wordpress. whenever you hit the paramalink of this static page this file will be called.
thus you can handle the everything mail content etc.
Thanks.