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.
Related
I'm trying add/remove Wordpress search filter according to the page author of the page I'm currently on. The search bar is in the theme header and therefore independent of the page I am on. I would like to use the author ID of the current page to add a filter for the next search I would do in the search bar.
This is an e-commerce site. The search bar is in the header (independent of the page). I'm on the shop page. I'd like to search products (i.e. posts with the same author ID) related to the shop page I was on before the search action.
I have written the code for functions.php however it doesn't work, i.e "my_search_filter" is not added (nor deleted). What I'm missing?
function search_filter_by_page_author()
{
$author_id = get_the_author_meta('ID');
#echo $author_id;
if (in_array($author_id, array("1", "2"))
{
add_filter( 'pre_get_posts', 'my_search_filter' );
}
else
{
remove_filter( 'pre_get_posts', 'my_search_filter' );
}
}
add_action( 'wp_head', 'search_filter_by_page_author' );
function my_search_filter( $query )
{
if ( $query->is_search && !is_admin())
{
$query->set( 'author', '1, 2' );
}
return $query;
}
Here is an answer that may be what you're after. In my opinion, you should
Set a new searchform.php
Add your pre_get_posts (which by the way is an action, and not a filter)
searchform.php (example) - This goes in your theme or child theme root folder.
<?php
global $post;
$author_id = $post->post_author;?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search" class="searchform search">
<div class="form-group">
<input type="text" class="form-control" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search …', 'text_domain' ); ?>"/>
<input type="hidden" name="author" value="<?php echo $author_id;?>">
</div>
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
</form>
Search filter (put in functions.php)
add_action('pre_get_posts', 'search_filter_by_page_author');
function search_filter_by_page_author($query){
if ($query->is_search && !is_admin() && isset($_GET['author'])) {
$query->set('author', $_GET['author']);
}
return $query;
}
This may not do exactly what you're looking for, but I think you should be able to get a positive result from this as a starting point.
In a nutshell what I'm trying to achieve is to let a user add some admin settings fields to a custom plugin via the backend and then populate them as required.
So the default settings page would have one Settings Field:
Location (with setting location_id) : (User enters location here and it gets stored in the database)
What I want to make happen is have a button that says "Add Another Location" which will automatically create a new settings field with an ID of location_id2. Adding a third would create a third field and the ID assigned sequentially. (Ideally I would like to give the option to remove any of the fields but that is a secondary requirement.)
These fields would be saved in the database so I can call them later in the front end using get_option('location_id');.
The code I have I currently have is for a single settings field
add_action('admin_menu', 'mycustom_menu');
function mycustom_menu() {
//create new top-level menu
add_menu_page('mycustom Connect', 'mycustom Connect', 'administrator', __FILE__, 'mycustom_settings_page' , plugins_url('/images/icon1.png', __FILE__) );
//call register settings function
add_action( 'admin_init', 'register_mycustom_settings' );
}
function register_mycustom_settings() {
//register our settings
register_setting( 'mycustom-settings-group', 'location_id' );
}
function mycustom_settings_page() {
?>
<div class="wrap">
<h1><strong></strong>mycustom Settings</strong></strong></h1>
<form autocomplete="off" method="post" action="options.php">
<?php settings_fields( 'mycustom-settings-group' ); ?>
<?php do_settings_sections( 'mycustom-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Location ID</th>
<td><input autocomplete="off" type="text" name="location_id" value="<?php echo esc_attr( get_option('location_id') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
Using this same code I can manually create many of these fields as I want but as the question states how can i let a backend user do this dynamically. I have searched through quite a few other questions and haven't been able to find anything that would even help me get started so any help would be greatly appreciated.
many thanks in advance!
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.
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'm using this piece of code to add a form to a Wordpress taxonomy:
function albums_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'albums' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','albums' ); ?></p>
</div>
<?php
}
add_action( 'albums_add_form_fields', 'albums_taxonomy_add_new_meta_field', 10, 2 );
The value is saving fine. However, how can I output on my template the value user filled out in the form ? What's the php function to use this value in front-end ?
Thanks.
Just taking a guess, on the page where the form is being redirected to try:
echo $_POST['term_meta']['custom_term_meta']