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
Related
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
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 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 :)
This is the php function present in function.php :
<?php
add_action('admin_menu', 'baw_create_menu');
function baw_create_menu() {
add_menu_page('Bubbles', 'Bubbles', 'administrator', __FILE__, 'baw_settings_page');
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
register_setting( 'baw-settings-group', 'category' );
}
function baw_settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<?php do_settings_sections( 'baw-settings-group' ); ?>
<textarea style="width:400px" name="category"><?php echo get_option('category'); ?></textarea>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
And I want to get the string :
var category = "<?php echo get_option('category'); ?>";
alert(category);
But the alert gives me my php code: <php echo get_option ('category');?> And not the string such as "banana"
var category = <?php echo json_encode(get_option('bulle_index_1')); ?> ;
alert( category );
http://www.php.net/json_encode
PHP cannot be parsed in a .js file. You need to create a function in your .js file and pass in the string returned from PHP:
function fromPHP(phpString) {
var category = phpString;
alert(category);
}
In your .php file:
fromPHP(<?php echo json_encode(get_option('bulle_index_1')); ?>);
Im using wordpress 3.8, woocommerce 2.0.20. And im using this Zoom Image Plugin(http://wordpress.org/plugins/zoom-image/)
Everything is looking fine, but when I have a product with color variation, it does not change the color of the image, but when you put your mouse to zoom, it appears the previous image.
Zoom Image Plugin code:
class TccZoom {
var $pluginPath;
var $pluginUrl;
public function __construct()
{
// Set Plugin Path
$this->pluginPath = dirname(__FILE__);
// Set Plugin URL
$this->pluginUrl = WP_PLUGIN_URL . '/zoom-image';
add_filter('woocommerce_product_thumbnails', array( &$this, 'apply_zoom') );
add_action('woocommerce_product_thumbnails', array( &$this, 'add_scripts') );
if(is_admin()){
add_action('admin_menu', array(&$this, 'add_zoom_image_plugin_page'));
add_action('admin_init', array(&$this, 'zoom_image_init'));
add_action( 'admin_enqueue_scripts', array(&$this, 'wp_enqueue_color_picker') );
}
}
static function install() {
add_option('zoom_image_options', array('zoom_thumbnails'=>'1'));
}
function wp_enqueue_color_picker( ) {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker-script', plugins_url('/js/colorPicker.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}
public function add_zoom_image_plugin_page(){
// This page will be under "Settings"
add_options_page('Zoom Image Settings', 'Zoom Image', 'manage_options', 'zoom-image', array($this, 'create_zoom_image_page'));
}
public function create_zoom_image_page(){
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Zoom Image Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields('zoom_image_group');
do_settings_sections('zoom_image_settings');
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function zoom_image_init(){
register_setting('zoom_image_group', 'zoom_image_options', array($this, 'check_zoom_image_options'));
add_settings_section(
'general_zoom_settings',
'Zoom image options',
array($this, 'print_section_info'),
'zoom_image_settings'
);
add_settings_field(
'zoom_thumbnails',
'Zoom over thumbnails ?',
array($this, 'create_zoom_thumbnails_field'),
'zoom_image_settings',
'general_zoom_settings'
);
/*
add_settings_field(
'zoom_level',
'Zoom level',
array($this, 'create_zoom_level_field'),
'zoom_image_settings',
'general_zoom_settings'
);
*/
add_settings_field(
'zoom_type',
'Zoom type',
array($this, 'create_zoom_inner_field'),
'zoom_image_settings',
'general_zoom_settings'
);
/*
add_settings_field(
'zoom_background_color',
'Background color',
array($this, 'create_zoom_color_field'),
'zoom_image_settings',
'general_zoom_settings'
);
*/
}
public function check_zoom_image_options($input){
if(!in_array($input['zoom_thumbnails'],array(0,1)))
{
$input['zoom_thumbnails'] = '';
}
if(!in_array($input['zoom_level'],array(0.5,1,2)))
{
$input['zoom_level'] = '';
}
if(!in_array($input['zoom_type'],array('window','inner','lens')))
{
$input['zoom_type'] = '';
}
return $input;
}
public function print_section_info(){
//print 'Enter your setting below:';
}
public function create_zoom_thumbnails_field(){
$options = get_option('zoom_image_options');
?>
<input type="checkbox" id="zoom_over_thumbnails" name="zoom_image_options[zoom_thumbnails]" value="1" <?php if($options['zoom_thumbnails']==1) { ?> checked="checked" <?php } ?> />
<?php
}
public function create_zoom_inner_field(){
$options = get_option('zoom_image_options');
?>
<select name="zoom_image_options[zoom_type]">
<option value="window" <?php if($options['zoom_type']=='window') { ?> selected="selected" <?php } ?>>Window</option>
<option value="lens" <?php if($options['zoom_type']=='lens') { ?> selected="selected" <?php } ?>>Lens</option>
<option value="inner" <?php if($options['zoom_type']=='inner') { ?> selected="selected" <?php } ?>>Inner</option>
</select>
<?php
}
public function create_zoom_level_field()
{
$options = get_option('zoom_image_options');
?>
<select name="zoom_image_options[zoom_level]">
<option value="1" <?php if($options['zoom_level']==1) { ?> selected="selected" <?php } ?>>Normal zoom</option>
<?php /*
<option value="0.5" <?php if($options['zoom_level']==0.5) { ?> selected="selected" <?php } ?>>Twice as big</option>
*/ ?>
<option value="2" <?php if($options['zoom_level']==2) { ?> selected="selected" <?php } ?>>Twice as small</option>
</select>
<?php
}
public function create_zoom_color_field()
{
$options = get_option('zoom_image_options');
?>
<input type="text" id="zoom_background" class="wp-color-picker-field" name="zoom_image_options[zoom_background_color]" value="<?php echo esc_attr($options['zoom_background_color']); ?>" />
<br />
<span>Available only for Zoom type: window</span>
<?php
}
function apply_zoom()
{
global $post;
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full', false, '' );
$options = get_option('zoom_image_options');
ob_start();
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.woocommerce-main-image img').attr('data-zoom-image','<?php echo $src[0]; ?>');
$('.woocommerce-main-image img').elevateZoom({
<?php if($options['zoom_level']) { ?>
zoomLevel : <?php echo strip_tags(trim($options['zoom_level'])); ?>,
<?php }else { ?>
zoomLevel : 1,
<?php } ?>
<?php
switch ($options['zoom_type'])
{
case "window":
?>
zoomType : "window",
lensShape : "square",
<?php
break;
case "lens":
?>
zoomType : "lens",
lensShape : "round",
<?php
break;
case "inner":
?>
zoomType : "inner",
cursor : "crosshair",
<?php
break;
default:
?>
zoomType : "window",
lensShape : "square",
<?php
break;
}
if(strlen(trim($options['zoom_background_color']))>1 && $options['zoom_type']=='window' ) {
?>
tint:true,
tintColour:'<?php echo esc_attr($options['zoom_background_color']); ?>',
tintOpacity:0.5
<?php
}
?>
});
<?php if($options['zoom_thumbnails']==1) { ?>
$('.thumbnails .zoom img').each(function(){
$(this).attr('data-zoom-image',$(this).parent().attr('href'));
});
$('.thumbnails .zoom img').elevateZoom({
zoomType : "window",
lensShape : "square",
lensSize : 20,
zoomWindowPosition: 16,
zoomWindowOffetx: 10,
<?php if($options['zoom_level']) { ?>
zoomLevel : <?php echo strip_tags(trim($options['zoom_level'])); ?>,
<?php }else { ?>
zoomLevel : 1,
<?php } ?>
<?php
if(strlen(trim($options['zoom_background_color']))>1) {
?>
tint:true,
tintColour:'<?php echo esc_attr($options['zoom_background_color']); ?>',
tintOpacity:0.5
<?php
}
?>
});
<?php } ?>
})
</script>
<?php
echo ob_get_clean();
}
function add_scripts() {
wp_enqueue_script( 'tcc-magnifier-js', $this->pluginUrl.'/js/jquery.elevateZoom-2.5.5.min.js', 'jquery' );
}
}
$tcczoom = new TccZoom;
register_activation_hook( __FILE__, array('TccZoom', 'install') );
We use something very similar, CloudZoom for WooCommerce, which has the same problem as well. If you are interested in switching plugins, you can solve the same problem for that plugin by adding this line of jQuery to a script in your header:
jQuery('form.variations_form').on( 'found_variation', function( event, variation ) {
addCloudZoom($productImages);
} );
Again, just to be clear, this solution won't work for the plugin you are using, but it solves the same problem for a plugin that provides identical functionality. If you can't find a solution for the plugin you're using, you might try switching and using this solution.