I wrote a very simple code that is supposed to read several fields from the WordPress admin section and display them on the page where I put the company code, but the problem is that only the field changes are displayed for the admin and the user sees no changes. Does not
I have two of them in my plugin that I put php code here
Part One: admin-page.php
if ( isset($_POST['submit']) ) {
$currency_update_date = isset($_POST['turksend_currency_update_date']) ? $_POST['turksend_currency_update_date'] : '';
update_option('turksend_currency_update_date', $currency_update_date);
}
<form method="POST">
<table class="form-table">
<tr>
<th scope="row">
<label for="turksend_currency_update_date">Date Update</label>
</th>
<td>
<input name="turksend_currency_update_date" type="text" id="turksend_currency_update_date" value="<?php echo get_option('turksend_currency_update_date', ''); ?>" class="regular-text code">
</td>
</tr>
<tr>
</table>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes'); ?>">
</p>
</form>
Part two: turksend-calculator.php
<?php
/**
* Plugin Name: ....
* Plugin URI: ....
* Description: .....
* Version: 1.0
* Author: ...
* Author URI: .....
*/
global $turksend_db_version;
$turksend_db_version = '1.0';
/* Install plugin - Create options */
function turksend_install() {
global $turksend_db_version;
add_option( 'turksend_db_version', $turksend_db_version );
add_option( 'turksend_currency_update_date', '' );
}
register_activation_hook( __FILE__, 'turksend_install' );
/* Uninstall plugin - Delete options */
function turksend_uninstall() {
global $turksend_db_version;
delete_option( 'turksend_db_version' );
delete_option( 'turksend_currency_update_date' );
}
register_deactivation_hook( __FILE__, 'turksend_uninstall' );
register_uninstall_hook( __FILE__, 'turksend_uninstall' );
/* Add admin menu page */
function turksend_init() {
require_once plugin_dir_path(__FILE__) . 'admin-page.php';
}
function turksend_register_options_page() {
add_menu_page(
'Calculate',
'Calculate',
'manage_options',
'turksend',
'turksend_init',
'dashicons-plus'
);
}
add_action('admin_menu', 'turksend_register_options_page');
/* Add plugin page settings link */
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'turksend_add_plugin_page_settings_link');
function turksend_add_plugin_page_settings_link ( $links ) {
$links[] = '' . __('Settings') . '';
return $links;
}
function turksend_canculate() {
echo get_option('turksend_currency_update_date');
}
add_shortcode('turksend-canculate', 'turksend_canculate');
Everything is correct, changes are not displayed only for users who are not logged in to the site
function turksend_canculate() {
echo get_option('turksend_currency_update_date');
}
you have an error, you must return data, not print
function bartag_func( $atts ) {
$a = shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts );
return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );
https://codex.wordpress.org/Shortcode_API
Related
I have a nice demo importer which is working fine but with a category post count issue so there is plugin which is fixing this and I'm trying to integrate into my demo importer library.
This plugin here is fixing the issue after demo import but requires to go into the plugin page selecting post types in order to click on submit button.
Well this is the plugin admin page:
<?php
if($_POST) {
if(!check_admin_referer('select_post_types_'.get_current_user_id() )){
echo 'That is not allowed'; exit;
}
$fixes = $_POST['fix'];
foreach($fixes AS $fix_id){
$fix = new Fix_Category_Count();
$fix->post_type = $fix_id;
if($fix->process()){
$updated = TRUE;
}
}
}
?>
<div>
<?php echo "<h2>" . __( 'Fix Category Count Settings', 'fix_category_count' ) . "</h2>"; ?>
<h3>Select Post Types to Fix</h3>
<form name="site_data_form" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<div class="sa_block">
<?php
$post_types = get_post_types(array('public'=>TRUE));
foreach($post_types AS $post_type){
?>
<input type="checkbox" class="post_type" name="fix[]" id="fix_<?=$post_type; ?>" value="<?=$post_type; ?>" /> <label for="fix_<?=$post_type; ?>"><?=ucwords(preg_replace("/_/"," ",$post_type)); ?> (<?=$post_type;?>)</label><br />
<?php
}
?>
<br><br>
Select All |
Deselect All
</div>
</div>
<?php wp_nonce_field('select_post_types_'.get_current_user_id()); ?>
<div class="submit">
<input type="submit" name="Submit" value="<?php _e('Fix Categories Now', '' ) ?>" />
</div>
<?php if($updated){ ?>
<div class="updated"><p><strong><?php _e('Categories Updated.' ); ?></strong></p></div>
<? } ?>
</form>
</div>
<script type="text/javascript">
if(jQuery){
jQuery(document).ready(function($){
$('.select_boxes').click(function(e){
e.preventDefault();
if($(this).attr('rel')=='all'){
$('.post_type').each(function() {
this.checked = true;
});
}
else{
$('.post_type').each(function() {
this.checked = false;
});
}
});
});
}
</script>
So the CLASS of the plugin is here
I want to use only the class without the admin page with something like this:
ob_start();
if ( 'complete' == $response['process'] ) {
// Set imported menus to registered theme locations
$locations = get_theme_mod( 'nav_menu_locations' ); // registered menu locations in theme
$menus = wp_get_nav_menus(); // registered menus
if ( $menus ) {
foreach( $menus as $menu ) { // assign menus to theme locations
if( 'Main Menu' == $menu->name ) {
$locations['primary'] = $menu->term_id;
break;
}
}
}
set_theme_mod( 'nav_menu_locations', $locations ); // set menus to locations
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
// I want to add the plugin functionality as a last process here!
}
ob_end_clean();
I'm not a php developer yet but I would like to get this working without fixing the category count using the plugin page options but directly on the function above and with the php class provided by plugin.
Thank you.
i am currently setting up an options page in wordpress for my plugin. Why is it not possible to implement the functions as protected or private? The class is directly instantiated. As far as is know, it is possible with java as object oriented language to make them protected in this scenario, isn´t it?
Do i have to implement the $this variable generally with array or is this wordpress specific? It works right now and i have found some comments in the doc claiming that it has to me made this way, but i don´t understand why it´s necessary.
I know this is mostly basic php knowledge, but that´s why i am asking - to learn. Thanks for any help
<?php
class SettingsPage {
static $optionGroup = 'juvo_option_group';
static $optionTitle = 'JUVO Anpassungen';
public function __construct() {
// create custom plugin settings menu
add_action('admin_menu', array( $this, 'my_cool_plugin_create_menu'));
//call register settings function
add_action( 'admin_init', array( $this, 'register_juvo_plugin_settings' ));
}
public function my_cool_plugin_create_menu() {
//create new top-level menu
add_options_page(
self::$optionTitle, //Page Title
self::$optionTitle, //Menu Title
'manage_options', //required Capabilities
'juvo-setting', //Slug
array( $this, 'juvo_settings_page')
);
}
public function register_juvo_plugin_settings() {
//register our settings
register_setting( self::$optionGroup, 'privacy_policy_comments' );
}
public function juvo_settings_page() {
?>
<div class="wrap">
<h1><?php echo self::$optionTitle ?></h1>
<form method="post" action="options.php">
<?php settings_fields( self::$optionGroup );
$options = get_option( 'privacy_policy_comments' );
do_settings_sections( self::$optionGroup ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Seitentitel Datenschutzerklärung</th>
<td><input type="text" name="privacy_policy_comments[title]" value="<?php echo esc_attr( $options['title']); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
}
if( is_admin() )
$my_settings_page = new SettingsPage();
I want to add text widget into my site sidebar when I activate theme. How can I do this using wordpress function or other methods? I'm using wordpress 4.1.
Help me. Thanks.
I don't know that what kind of widget you want to create, so i give you a simple example so you can understand.
Write blow code in functions.php or create an external file and include it into functions.php
First Register the Widget:
//========= Your Custom Widget
add_action( 'widgets_init', 'your_simple_widget' );
function your_simple_widget() {
register_widget( 'Your_Simple_Same_Widget' );
}
Second Create a widget body:
class Your_Simple_Same_Widget extends WP_Widget { }
NOTE: register_widget( 'Your_Simple_Same_Widget' ); name and class Your_Simple_Same_Widget and widget setup name must be same
There are four parts of widget in class
Widget Setup
How to display on front end
Update Widget values
Widget form or fields in admin panel
Ok Third in class Write this code like:
//========= Your Custom Widget Body
class Your_Simple_Same_Widget extends WP_Widget {
//=======> Widget setup
function Your_Simple_Same_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'your class', 'description' => __('Your Class Description widget', 'your class') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'your_widget' );
/* Create the widget. */
$this->WP_Widget( 'your_widget', __('Widget Heading', 'your class'), $widget_ops, $control_ops );
}
//=======> How to display the widget on the screen.
function widget($args, $widgetData) {
extract($args);
//=======> Our variables from the widget settings.
$your_widget_title = $widgetData['txtYourTitle'];
$your_widget_description = $widgetData['txtYourDescription'];
//=======> widget body
echo $before_widget;
echo '<div class="">';
if($your_widget_title){
echo '<h2>'.$your_widget_title .'</h2>';
}
if($your_widget_description){
echo '<p>'.$your_widget_description .'</p>';
}
echo "</div>";
echo $after_widget;
}
//=======>Update the widget
function update($new_widgetData, $old_widgetData) {
$widgetData = $old_widgetData;
//Strip tags from title and name to remove HTML
$widgetData['txtYourTitle'] = $new_widgetData['txtYourTitle'];
$widgetData['txtYourDescription'] = $new_widgetData['txtYourDescription'];
return $widgetData;
}
//=======>widget Display
function form($widgetData) {
//Set up some default widget settings.
$widgetData = wp_parse_args((array) $widgetData);
?>
<p>
<label for="<?php echo $this->get_field_id('txtYourTitle'); ?>">Widget Title:</label>
<input id="<?php echo $this->get_field_id('txtYourTitle'); ?>" name="<?php echo $this->get_field_name('txtYourTitle'); ?>" value="<?php echo $widgetData['txtYourTitle']; ?>" style="width:275px;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('txtYourDescription'); ?>">Widget Title:</label>
<input id="<?php echo $this->get_field_id('txtYourDescription'); ?>" name="<?php echo $this->get_field_name('txtYourDescription'); ?>" value="<?php echo $widgetData['txtYourDescription']; ?>" style="width:275px;" />
</p>
<?php
}
}
?>
I hope this may help you.
hi to day start to create the setting API write coding to create simple option page for my theme this is my full code
when run the my code it is appear this error
Warning: Illegal string offset 'jw_banner_heading' in C:\wamp\www\wp39\wp-content\plugins\JW_Options\jw_options.php on line 65
please how can solve this problem??
<?php
class JW_Options {
private $options;
public function __construct() {
add_action('admin_menu', array($this, 'add_menu_page') );
add_action('admin_init', array($this, 'register_settings_and_fields'));
//get_option($option_name);
$this->options = get_option('jw_plugin_options');
}
public function add_menu_page() {
// it is add the menu page to the settings page in the dashboard
//add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, array($this, 'display_options_page'));
}
public function display_options_page() {
?>
<div class="wrap">
<h2>MY Theme Options</h2>
<form action="options.php" method="post" enctype="multipart/form-data">
<!-- outputs action and options_page fields for a setting page -->
<!-- settings_fields( $option_group ); -->
<?php settings_fields( 'jw_plugin_options' ); ?>
<!-- do_settings_sections( $page ); -->
<?php do_settings_sections( __FILE__ ); ?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Settings">
</p>
</form>
</div>
<?php
}
public function register_settings_and_fields() {
//delete_option('jw_plugin_options');
//register_setting( $option_group, $option_name, $sanitize_callback );
register_setting('jw_plugin_options', 'jw_plugin_options' );
//add_settings_section( $id, $title, $callback, $page );
add_settings_section('jw_main_section', 'Main Settings', array($this, 'jw_main_section_cb'), __FILE__);
//add_settings_field( $id, $title, $callback, $page, $section, $args );
add_settings_field('jw_banner_heading', 'Banner Heading', array($this, 'jw_banner_heading_setting'), __FILE__, 'jw_main_section');
add_settings_field('jw_logo', 'Your Logo', array($this, 'jw_logo_setting'), __FILE__, 'jw_main_section');
}
public function jw_main_section_cb() {
}
public function jw_banner_heading_setting() {
echo "<input type='text' name='jw_plugin_options[jw_banner_heading]' id='jw_banner_heading' value='{$this->options['jw_banner_heading']}' />";
}
public function jw_logo_setting() {
echo '<input type="file" name="jw_logo_upload" />';
}
}
if( is_admin() )
$JW_Options = new JW_Options();
?>
Create a sanitize callback function
Click on the save button
I just created a simple theme option page that is working fine and also saved after when press save options.
But when I go somewhere else from theme options page and come back to theme options page that settings what I saved just disappear and I have to change that again whenever I come to theme options page.
Here is my code
add_action( 'admin_menu', 'theme_options_add_page' );
if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
add_option('new_theme_options', array (
'sidebar2_on' => true,
'footer_text' => ''
));
$theme_options = get_option('new_theme_options');
}
function theme_options_add_page() {
add_submenu_page( 'themes.php', 'My Theme Options', 'Theme Options', 8, 'themeoptions', 'theme_options_do_page' );
}
function theme_options_do_page() {
global $theme_options;
$new_values = array (
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'responsivetheme' ) . "</h2>"; ?>
<form method="post" action="themes.php?page=themeoptions">
<label for="footer_text">Footer Text:</label>
<input id="footer_text" type="text" name="footer_text" value="<?php echo $theme_options['footer_text']; ?>" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
</p>
</form>
</div>
<?php
}
#Praveen answer is correct, but for completeness I'll post the full code I tested. Please, note that you should always develop with WP_DEBUG enabled. It shows three issues with your code:
using $_POST['footer_text'] without it being defined (Praveen's answer)
using the deprecated function get_current_theme()
using Level instead of Capability in add_submenu_page()
I dropped the following code into my theme's functions.php and it works ok:
if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
$theme_options = array (
'sidebar2_on' => true,
'footer_text' => ''
);
add_option( 'new_theme_options', $theme_options );
}
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_add_page() {
add_submenu_page(
'themes.php',
'My Theme Options',
'Theme Options',
'add_users',
'themeoptions',
'theme_options_do_page'
);
}
function theme_options_do_page() {
global $theme_options;
if( isset( $_POST['footer_text'] ) ) {
$new_values = array (
'footer_text' => htmlentities( $_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . wp_get_theme() . __( ' Theme Options', 'responsivetheme' ) . "</h2>"; ?>
<form method="post" action="themes.php?page=themeoptions">
<label for="footer_text">Footer Text:</label>
<input id="footer_text" type="text" name="footer_text" value="<?php echo $theme_options['footer_text']; ?>" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
</p>
</form>
</div>
<?php
}
The only issue I can see is the option value sidebar2_on that's being overwritten in theme_options_do_page(), but your sample code does not show it being used elsewhere.
Please change this code:
if($_POST['footer_text']) {
$new_values = array (
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}
hope this will work fine :)