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.
Related
I am having a trouble with my website's pop-up widget. The problem is the pop-up appears when you enter or refresh the website but I can not close it. I click on the "X" button but nothing happens. The code:
<?php
/*
Plugin Name: WP Welcome Message
Plugin URI: http://www.a1netsolutions.com/Products/WP-Welcome-Message
Description: <strong>WP Welcome Message</strong> is a wordpress plugin, which help your to make any announcement, special events, special offer, signup message or such kind of message, displayed upon your website's visitors when the page is load through a popup box.
Version: 3.0
Author: Ahsanul Kabir
Author URI: http://www.ahsanulkabir.com/
License: GPL2
License URI: license.txt
*/
$wpwm_conf = array(
'VERSION' => get_bloginfo('version'),
'VEWPATH' => plugins_url('lib/', __FILE__),
);
function wpwm_admin_styles()
{
global $wpwm_conf;
wp_enqueue_style('wpwm_admin_styles',($wpwm_conf["VEWPATH"].'css/admin.css'));
if( $wpwm_conf["VERSION"] > 3.7 )
{
wp_enqueue_style('wpwm_icon_styles',($wpwm_conf["VEWPATH"].'css/icon.css'));
}
}
add_action('admin_print_styles', 'wpwm_admin_styles');
function wpwm_scripts_styles()
{
global $wpwm_conf;
$wpwmBoxSetly = get_option('wpwm_boxsetly');
if(!$wpwmBoxSetly){$wpwmBoxSetly=="fadeOut";}
wp_enqueue_script('wpwm_site_scripts',($wpwm_conf["VEWPATH"].'js/site_'.$wpwmBoxSetly.'.js'),array('jquery'),'',true);
wp_enqueue_style('wpwm_site_style',($wpwm_conf["VEWPATH"].'css/site.css'));
}
add_action('wp_enqueue_scripts', 'wpwm_scripts_styles');
function wpwm_defaults()
{
$wpwm_default = plugin_dir_path( __FILE__ ).'lib/default.php';
if(is_file($wpwm_default))
{
require $wpwm_default;
foreach($default as $k => $v)
{
$vold = get_option($k);
if(!$vold)
{
update_option($k, $v);
}
}
if(!is_multisite())
{
unlink($wpwm_default);
}
}
}
function wpwm_activate()
{
$wpwm_postsid = get_option( 'wpwm_postsid' );
if(!$wpwm_postsid)
{
$inputContent = 'Welcome to '.get_bloginfo('name').', '. get_bloginfo('description');
$new_post_id = wpwm_printCreatePost($inputContent);
update_option( 'wpwm_postsid', $new_post_id );
}
wpwm_defaults();
}
function wpwm_redirect()
{
$wpwm_fv = get_option('wpwm_fv');
if($wpwm_fv != 'fv')
{
echo 'Please setup your <strong>WP Welcome Message 2.0</strong> plugin. <input type="submit" value="Setup" class="button" />';
}
}
add_action( 'admin_footer', 'wpwm_redirect' );
function wpwm_admin_menu()
{
global $wpwm_conf;
if( $wpwm_conf["VERSION"] < 3.8 )
{
add_menu_page('WP Welcome Message', 'Welcome Msg', 'manage_options', 'wpwm_admin_page', 'wpwm_admin_function', (plugins_url('lib/img/icon.png', __FILE__)));
}
else
{
add_menu_page('WP Welcome Message', 'Welcome Msg', 'manage_options', 'wpwm_admin_page', 'wpwm_admin_function');
}
}
add_action('admin_menu', 'wpwm_admin_menu');
function wpwm_select( $iget, $iset, $itxt )
{
if( $iget == $iset )
{
echo '<option value="'.$iset.'" selected="selected">'.$itxt.'</option>';
}
else
{
echo '<option value="'.$iset.'">'.$itxt.'</option>';
}
}
function wpwm_update($key, $value)
{
if(isset($value) && !empty($value))
{
update_option($key, $value);
}
}
function wpwm_admin_function()
{
$wpwm_fv = get_option('wpwm_fv');
if($wpwm_fv != 'fv')
{
update_option('wpwm_fv', 'fv');
}
wpwm_update('wpwm_loc', $_POST["wpwm_loc"]);
wpwm_update('wpwm_log', $_POST["wpwm_log"]);
wpwm_update('wpwm_boxsetly', $_POST["wpwm_boxsetly"]);
wpwm_update('wpwm_bgstyle', $_POST["wpwm_bgstyle"]);
wpwm_update('wpwmTemplate', $_POST["wpwmTemplate"]);
wpwm_update('wpwm_onlyFirstVisit', $_POST["wpwm_onlyFirstVisit"]);
wpwm_update('wpwm_ststs', $_POST["wpwm_ststs"]);
$wpwmPID = get_option('wpwm_postsid');
wpwm_updatePost($_POST["wpwmeditor"], $wpwmPID);
if( isset($_POST["wpwmeditor"]) || isset($_POST["wpwmTemplate"]) )
{
echo '<div id="message" class="updated wpwm_updated"><p>Your data has been successfully saved.</p></div>';
}
global $wpwm_conf;
echo '<div id="wpwm_container">
<div id="wpwm_main">
<img src="',$wpwm_conf["VEWPATH"],'/img/uvg.png" id="wpwm_uvg" />
<h1 id="wpwm_page_title">WP Welcome Message</h1>';
?>
<div class="wpwm_box">
<div class="wpwm_box_title">Your Welcome Message
<form method="post" action="" id="wpwm_off_on"><input type="hidden" name="wpwm_ststs" value="<?php
$wpwm_ststs = get_option('wpwm_ststs');
if($wpwm_ststs == 'on')
{
echo 'off';
}
else
{
echo 'on';
}
?>" /><input type="image" src="<?php echo $wpwm_conf["VEWPATH"]; ?>/img/<?php
$wpwm_ststs = get_option('wpwm_ststs');
if($wpwm_ststs == 'on')
{
echo 'one-check_yes';
}
else
{
echo 'one-check_no';
}
?>.png" /></form>
</div>
<div class="wpwm_box_con">
<form method="post" action="" id="wpwm_content_form">
<?php
$wpwm_ststs = get_option('wpwm_ststs');
if($wpwm_ststs == 'off')
{
echo '<div id="wpwm_content_disable"></div>';
}
$wpwmPID = get_option('wpwm_postsid');
$wpwmContent = get_post($wpwmPID);
$wpwmContent = $wpwmContent->post_content;
$wpwmContent = apply_filters('the_content', $wpwmContent);
$wpwmContent = str_replace(']]>', ']]>', $wpwmContent);
if( $wpwm_conf["VERSION"] < 3.3 )
{
echo '<textarea name="wpwmeditor" style="width:100%; height:300px;"></textarea>';
}
else
{
wp_editor( $wpwmContent, 'wpwmeditor', array('textarea_rows' => 20, 'textarea_name' => 'wpwmeditor') );
}
?>
<input type="submit" value="save changes" />
</form>
</div>
</div>
<div class="wpwm_box">
<div class="wpwm_box_title">Settings</div>
<div class="wpwm_box_con">
<form method="post" action="">
<div class="row">
<label>On Which Page/Pages to Display : </label>
<select name="wpwm_loc">
<?php
$wpwmLoc = get_option( 'wpwm_loc' );
wpwm_select( $wpwmLoc, 'home', 'Home Page Only' );
wpwm_select( $wpwmLoc, 'all', 'All Pages' );
?>
</select>
</div>
<div class="row">
<label>Logged-in / Not Logged-in user : </label>
<select name="wpwm_log">
<?php
$wpwm_log = get_option( 'wpwm_log' );
wpwm_select( $wpwm_log, 'log', 'Logged-in Users Only' );
wpwm_select( $wpwm_log, 'nlog', 'Not Logged-in Users Only' );
wpwm_select( $wpwm_log, 'all', 'For All' );
?>
</select>
</div>
<div class="row">
<label>Message Box Animation Style : </label>
<select name="wpwm_boxsetly">
<?php
$wpwmBoxSetly = get_option( 'wpwm_boxsetly' );
wpwm_select( $wpwmBoxSetly, 'fadeOut', 'Fade Out' );
wpwm_select( $wpwmBoxSetly, 'slideUp', 'Slide Up' );
?>
</select>
</div>
<div class="row">
<label>Template : </label>
<select name="wpwmTemplate">
<?php
$wpwmTemplate = get_option( 'wpwmTemplate' );
wpwm_select( $wpwmTemplate, 'black-color', 'Dark Color Only' );
wpwm_select( $wpwmTemplate, 'black-white-color', 'White Color Only' );
wpwm_select( $wpwmTemplate, 'white-color', 'Full White Color Only' );
wpwm_select( $wpwmTemplate, 'black-striped', 'Dark Stripes' );
wpwm_select( $wpwmTemplate, 'black-white-striped', 'White Stripes' );
wpwm_select( $wpwmTemplate, 'white-striped', 'Full White Stripes' );
wpwm_select( $wpwmTemplate, 'bootstrap', 'Bootstrap Style' );
?>
</select>
</div>
<div class="row">
<label>Only For Fist Time Visit : </label>
<select name="wpwm_onlyFirstVisit">
<?php
$wpwm_onlyFirstVisit = get_option( 'wpwm_onlyFirstVisit' );
wpwm_select( $wpwm_onlyFirstVisit, 'on', 'Enable' );
wpwm_select( $wpwm_onlyFirstVisit, 'off', 'Disable' );
?>
</select>
</div>
<input type="submit" value="save changes" />
</form>
</div>
</div>
<?php
echo '</div>
<div id="wpwm_side">
<div class="wpwm_box">';
echo '<img src="',$wpwm_conf["VEWPATH"],'/img/wp-advert-1.png" />';
echo '</div><div class="wpwm_box">';
echo '<img src="',$wpwm_conf["VEWPATH"],'/img/wp-advert-2.png" />';
echo '</div>
</div>
<div class="wpwm_clr"></div>
</div>';
}
function wpwm_content()
{
$wpwm_ststs = get_option('wpwm_ststs');
if($wpwm_ststs == 'on')
{
$wpwm_onlyFirstVisit = get_option( 'wpwm_onlyFirstVisit' );
if( $wpwm_onlyFirstVisit == "on" )
{
if( (!isset($_SESSION["wpwm_session"])) || ($_SESSION["wpwm_session"] != 'off') )
{
wpwm_popupFirst();
}
}
else
{
wpwm_popupFirst();
}
}
}
function wpwm_popupFirst()
{
$wpwm_loc = get_option( 'wpwm_log' );
if(get_option('wpwm_ststs') == 'on')
{
if( $wpwm_loc == 'log' )
{
if ( is_user_logged_in() )
{
wpwm_popupCheckPage();
}
}
elseif( $wpwm_loc == 'nlog' )
{
if ( !is_user_logged_in() )
{
wpwm_popupCheckPage();
}
}
else
{
wpwm_popupCheckPage();
}
}
}
function wpwm_popupTemp()
{
$wpwmPID = get_option( 'wpwm_postsid' );
$wpwmTemplate = get_option('wpwmTemplate');
$content_post = get_post($wpwmPID);
$wpwmContent = $content_post->post_content;
$wpwmContent = apply_filters('the_content', $wpwmContent);
$wpwmContent = str_replace(']]>', ']]>', $wpwmContent);
$session_id = session_id();
echo '
<div id="wpwm_hideBody" class="'.$wpwmTemplate.'-body">
<div id="wpwm_popBoxOut">
<div class="wpwm-box">
<div id="wpwm_popBox">
<span id="wpwm_popClose">×</span>
'.$wpwmContent.'
<div class="cl_fix"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function()
{
jQuery("html, body").css({"overflow": "hidden"});
});
</script>
';
echo '<span>',get_option('wpwm_dev1'),get_option('wpwm_dev2'),get_option('wpwm_dev3'),'</span>';
}
function wpwm_popupCheckPage()
{
if( ( get_option( 'wpwm_loc' ) ) == 'home' )
{
if( is_front_page() )
{
wpwm_popupTemp();
}
}
else
{
wpwm_popupTemp();
}
}
function wpwm_sessionID()
{
if(!isset($_SESSION)){session_start();}
if(isset($_SESSION["wpwm_session"]))
{
$_SESSION["wpwm_session"] = 'off';
}
else
{
$_SESSION["wpwm_session"] = 'on';
}
}
add_action( 'wp_head', 'wpwm_sessionID' );
function wpwm_posts_init()
{
$args = array
(
'public' => false,
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'rewrite' => array( 'slug' => 'wpwmposts' ),
'capability_type' => 'post',
'has_archive' => false,
'supports' => array( 'title', 'editor', 'excerpt' )
);
register_post_type( 'wpwmposts', $args );
}
add_action( 'init', 'wpwm_posts_init' );
function wpwm_getCurrentUser()
{
if (function_exists('wp_get_current_user'))
{
return wp_get_current_user();
}
else if (function_exists('get_currentuserinfo'))
{
global $userdata;
get_currentuserinfo();
return $userdata;
}
else
{
$user_login = $_COOKIE["USER_COOKIE"];
$current_user = $wpdb->get_results("SELECT * FROM `".$wpdb->users."` WHERE `user_login` = '".$user_login."' ;");
return $current_user;
}
}
function wpwm_printCreatePost($inputContent)
{
$newPostAuthor = wpwm_getCurrentUser();
$newPostArg = array
(
'post_author' => $newPostAuthor->ID,
'post_content' => $inputContent,
'post_status' => 'publish',
'post_type' => 'wpwmposts'
);
$new_post_id = wp_insert_post($newPostArg);
return $new_post_id;
}
function wpwm_updatePost($inputContent, $id)
{
$newPostAuthor = wpwm_getCurrentUser();
$newPostArg = array
(
'ID' => $id,
'post_author' => $newPostAuthor->ID,
'post_content' => $inputContent,
'post_status' => 'publish',
'post_type' => 'wpwmposts'
);
$new_post_id = wp_insert_post($newPostArg);
return $new_post_id;
}
add_action('wp_footer', 'wpwm_content', 100);
register_activation_hook(__FILE__, 'wpwm_activate');
?>
Finally, I managed to find where the problem is.
echo '
<div id="wpwm_hideBody" class="'.$wpwmTemplate.'-body">
<div id="wpwm_popBoxOut">
<div class="wpwm-box">
<div id="wpwm_popBox">
<span id="wpwm_popClose">×</span>
'.$wpwmContent.'
<div class="cl_fix"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function()
{
jQuery("html, body").css({"overflow": "hidden"});
});
</script>
';
And find out there is no close function going on here:
<span id="wpwm_popClose">×</span>
So changed it with this:
<span id="wpwm_popClose" onclick="document.getElementById('pwm_hideBody').style.display='none'">×</span>
but when I edit this PHP code, WordPress gives me this error:
Parse error: syntax error, unexpected 'pwm_hideBody' (T_STRING), expecting ',' or ';' in /var/www/vhosts/derinuzay.org/httpdocs/wp-content/plugins/wp-welcome-message/wp-welcome-message.php on line 337
Could you please help me out about this error?
Try to add this:
jQuery('#wpwm_popClose').click(function() {
jQuery('#wpwm_hideBody').css('display', 'none');
});
inside the:
jQuery(document).ready(function() {
jQuery("html, body").css({"overflow": "hidden"});
});
What happens if you replace
echo '
<div id="wpwm_hideBody" class="'.$wpwmTemplate.'-body">
<div id="wpwm_popBoxOut">
<div class="wpwm-box">
<div id="wpwm_popBox">
<span id="wpwm_popClose">×</span>
'.$wpwmContent.'
<div class="cl_fix"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function()
{
jQuery("html, body").css({"overflow": "hidden"});
});
</script>
';
With:
?>
<div id="wpwm_hideBody" class="<?php echo $wpwmTemplate; ?>-body">
<div id="wpwm_popBoxOut">
<div class="wpwm-box">
<div id="wpwm_popBox">
<span id="wpwm_popClose">×</span>
<?php echo $wpwmContent; ?>
<div class="cl_fix"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#wpwm_popClose').click(function() {
jQuery('#wpwm_hideBody').css('display', 'none');
});
});
</script>
<?php
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'm two or three weeks old to Wordpress, I'm writing a tiny custom system as a plugin for a wordpress-based site. This systems reads a set of json encoded files and shows them as a table list.
Together with some basic settings that I'm already saving, I want to include a simple text editor for these json files, and I was wondering if there's a way to 'attach' a custom action for the options.php script that processes form submission, in order to send to it the file path and content.
I'm currently thinking in writing some ajax, but I prefer to ask here before, maybe there is a simple way to achieve this.
myplugin.php
function myplugins_menu(){
add_options_page('myplugin configuration','myplugin','manage_options', _
'myplugin_menu','myplugin_options');
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
add_action('admin_menu','myplugin_menu');
function myplugin_options(){
include('admin/myplugin-admin.php');
}
function register_mysettings() {
//register our settings
$setting_list=array('mail_title','mail_from','mail_to','recipient');
foreach ($setting_list as $setting){
register_setting( 'myplugin-settings-group', $setting );
}
}
myplugin-admin.php
<div class="wrap">
<h2>My plugin</h2>
<h3>My plugin Options</h3>
<form method="post" action="options.php">
<?php settings_fields( 'myplugin-settings-group' ); ?>
<?php do_settings_sections( 'myplugin-settings-group' ); ?>
<table class="form-table">
<?php
print_option('Mail Subject','mail_title');
print_option('Sender address','mail_from');
print_option('Recipient address','mail_to');
print_option('Recipient name','recipient');
?>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
echo "<select name=\"filename\" size=\"1\">";
$dir = plugin_dir_path( __FILE__ );
$files = glob($dir."../services/*.json");
foreach ($files as $filename){
echo "<option value=\"".basename($filename)."\">".basename($filename)."</option>";
}
echo "</select>";
$dir = plugin_dir_path( __FILE__ );
$file = file(WP_PLUGIN_DIR."/myplugin/services/010.Luftansa.json");
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
In the snippet above,
I put _SERVER['PHP_SELF'] as the action for the form, but this turns into /wp/wp-admin/options-general.php;.
If I put a path to my admin file I can't return to the admin interface except by an http-refresh or something like that.
Thanks for reading :)
myplugin-admin.php
<?php
/**
* #package my_plugin
* #version 1.6
*/
/*
Plugin Name: my plugin
Plugin URI:
Description:
Author:
Version:
Author URI:
*/
include_once(WP_PLUGIN_DIR."/my_plugin/model.php");
function my_plugin_admin_scripts(){
?>
<script type='text/javascript'>
jQuery('select')
.change(function() {
var filename = '';
jQuery('select option:selected').each(function() {
filename += jQuery( this ).text() + ' ';
});
jQuery.ajax({
type: 'GET',
url: '<?php echo WP_PLUGIN_URL;?>/my_plugin/action.php',
data: 'action=' + 'getJsonFile'+'&file='+filename,
success: function(data) {
jQuery('#services_file').val(data);
},
error: function(data) {
jQuery('#services_file').val('Something went wrong while loading '+filename+' file content');
}
});
})
.trigger('change');
</script>
<?php
}
add_action( 'admin_footer', 'my_plugin_admin_scripts' );
function print_option($label,$opt_name){
$str = "
<tr valign=\"top\">
<th scope=\"row\">{$label}</th>
<td><input type=\"text\" name=\"{$opt_name}\" value=\"".get_option($opt_name)."\" /></td>
</tr>";
echo $str;
}
function my_plugin_admin_tabs( $current = 'general' ) {
$tabs = array( 'general' => 'General', 'services' => 'Services');
echo "<h3>My plugin Options</h3>";
echo '<div id="icon-themes" class="icon32"><br></div>';
echo '<h2 class="nav-tab-wrapper">';
foreach( $tabs as $tab => $name ){
$class = ( $tab == $current ) ? ' nav-tab-active' : '';
echo "<a class='nav-tab$class' href='?page=my_plugin_menu&tab=$tab'>$name</a>";
}
echo '</h2>';
}
function tab_content_general() {
?>
<form method="post" action="options.php">
<?php settings_fields( 'my_plugin-settings-group' ); ?>
<?php do_settings_sections( 'my_plugin-settings-group' ); ?>
<table class="form-table">
<?php
print_option('Mail Subject','mail_title');
print_option('Sender address','mail_from');
print_option('Recipient address','mail_to');
print_option('Recipient name','recipient');
?>
</table>
<?php submit_button(); ?>
</form>
<?php
}
function tab_content_services(){
$files = glob(WP_PLUGIN_DIR."/my_plugin/services/*.json");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=my_plugin_menu&tab=services">
<table class="form-table">
<tr><td><select name="filename" size="1" style="width: 180px">
<?php
foreach ($files as $filename){
echo "<option value=\"".basename($filename)."\">".basename($filename)."</option>";
}
?>
</select>
<input name="Submit" type="submit" value="Update" />
</td></tr>
<tr><td><textarea id="services_file" name="update" cols="80" rows="18" style="font-family: monospace">
</textarea></td></tr>
</table>
</form>
<?php
}
$tab = ( isset ( $_GET['tab'] ) )?$_GET['tab']:"general";
my_plugin_admin_tabs($tab);
?>
<div class="wrap">
<?php
switch ($tab) {
case "general":
tab_content_general();
break;
case "services":
tab_content_services();
break;
default:
break;
}
?>
</div>
<?php
if($_POST['Submit']){
save_json_file($_POST['filename'],$_POST['update']);
}
?>
action.php
<?php
define( 'WP_USE_THEMES', false );
require_once( '../../../wp-load.php' );
require_once('model.php');
//main
if (isset($_GET['action'])){
$action = $_GET['action'];
}
if (isset($_POST['action'])){
$action = $_POST['action'];
}
if (isset($action)) {
switch ($action){
case 'getJsonFile':
getJsonFile($_GET['file']);
break;
default:
echo "action unknown";
break;
}
}
?>
model.php
<?php
function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
function read_plain_json($file){
$file = file(WP_PLUGIN_DIR."/my_plugin/services/".$file);
return implode("",$file);
}
function getJsonFile($file){
echo read_plain_json($file);
die();
}
function save_json_file($file,$content){
$open = fopen(WP_PLUGIN_DIR."/my_plugin/services/".$file,"w+");
fwrite($open, stripslashes($content));
fclose($open);
}
?>
my_plugin/services/010.Luftansa.json
[
{
"subcat": "Natural Nail Care",
"column": ["service","Mani","Pedi","Mani & Pedi","info","book"],
"service": [
["Deluxe","40.00","55.00","90.00","Service Information","Book Deluxe"],
["Express","30.00","45.00","71.00","","BookExpress"],
["Fully Loaded","55","70","119.00","ServiceInformation","Book Fully Loaded"],
["French","45","60","100.00","","Book French"],
["Pure and Natural","50","60","105.00","Service Information","Book Pure And Natural"],
["Deluxe Series of 6","216","297","487.00","","Book Deluxe Series Of 6"],
["Re-varnish and Tidy-up Hands","18","20","35","","Book Re-Varnish And Tidy-Up Hands"],
["Course of 6","","","","","Book Course Of 6"]
]
}
the server gave me this error but i can't understand why!
The error is
"Parse error: syntax error, unexpected end of file in /homepages/43/d514350324/htdocs/wp-content/plugins/NetClass/nw_NetClass_init_CustomBoxes.php on line 227"
Line 227 is the last line of the file.
That's the code. Help me please!
<?php
/* Define the custom box */
add_action( 'add_meta_boxes', 'aggiungiMetaBox' );
add_action( 'save_post', 'salvaMetaBox', 10, 2 );
add_action( 'wp_ajax_nc_savePortable_lezioni_action', 'nc_savePortable_lezioni' );
add_action( 'admin_footer', 'nc_savePortable_lezioni_js' );
function parseInt($str){
return (int) $str;
}
function aggiungiMetaBox(){
add_meta_box( "nw_Netclass_Box_AggiungiaCorso", "Aggiungi la lezione al corso", "aggiungiMetaBox_AggiungiLezione", "corso");
add_meta_box("nw_NetClass_Box_DatiFattura", "Dati della fattura", "aggiungiMetaBox_DatiFattura", "fattura");
add_meta_box("nw_NetClass_Box_Corsi_Associati", "Corsi associati", "aggiungiMetaBox_Corsi_Associati", "lezione");
}
function aggiungiMetaBox_Corsi_Associati($post){
?>
<p><?php echo get_post_meta($post->ID, 'corsi_associati',true); ?></p>
<?php
}
function is_in_array($array_lezioni_del_corso, $object){
if(!is_array($array_lezioni_del_corso)){
if($array_lezioni_del_corso == $object){
return true;
}else{
return false;
}
}
else{
return in_array($object, $array_lezioni_del_corso);
}
}
function aggiungiMetaBox_DatiFattura($post){
$post_id = $post->ID;
$user_info = get_userdata(get_post_meta($post_id, "intestatarioFattura" , true));
$user_meta = array(
'tipo_utente' => get_user_meta(get_post_meta($post_id, "intestatarioFattura" , true), 'tipo_utente',true),
'p_iva' => get_user_meta(get_post_meta($post_id, "intestatarioFattura" , true), 'p_iva',true),
'indirizzo_fatturazione' => get_user_meta(get_post_meta($post_id, "intestatarioFattura" , true), 'indirizzo_fatturazione',true)
);
?>
Intestatario della fattura : <?php echo $user_info->display_name; ?> <br>
Tipo di utente : <?php echo $user_meta['tipo_utente']; ?> <br>
Indirizzo di fatturazione : <?php echo $user_meta['indirizzo_fatturazione']; ?> <br>
P.IVA : <?php echo $user_meta['p_iva']; ?> <br>
Inponibile della fattura : <?php echo get_post_meta( $post_id, "inponibileFattura" , true); ?> <br>
Totale della fattura : <?php echo get_post_meta( $post_id, "totaleFattura" , true); ?> <br>
Articoli acquistati:
<ul>
<?php
foreach(get_post_meta( $post_id, "articoliFattura" , true) as $prodotto){
?>
<li><?php echo get_the_title($prodotto); ?></li>
<?php
}
?>
</ul>
<?php
}
//save the meta box
function salvaMetaBox($post_id)
{
//Salvo i dati del Box lezioni nel post corso //
global $wpdb;
$database_name = $wpdb->prefix.'Netclass_friends';
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || ( defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']) ) { return $post_id; }
//Salvo i dati delle fatture
}
function aggiungiMetaBox_AggiungiLezione($post){
global $wpdb;
$post_id = $post->ID;
$query = "SELECT post_title,ID FROM ".$wpdb->prefix."posts WHERE post_type='lezione' AND post_status='publish'";
$array_lezioni_del_corso = get_post_meta($post_id, 'array_lezioni_del_corso');
$lezioni = $wpdb->get_results($query);
foreach ($lezioni as $lezione) { ?>
<input id="<?php echo $lezione->ID; ?>" data-title="<?php echo $lezione->post_title; ?>" class="nc_lezione_pubblicata" type="checkbox" name="checkfield[]" value="<?php echo $lezione->ID; ?>" <?php if ( is_in_array($array_lezioni_del_corso[0], $lezione->ID)) { ?> checked <?php } ?>/>
<label for="<?php echo $lezione->ID; ?>"><?php echo $lezione->post_title; ?></label> <br>
<?php } ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function($)
{
$( '.sortable' ).sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.hndle',
placeholder: {
element: function(currentItem) {
return $("<li style='background:#E7E8AD'> </li>")[0];
},
update: function(container, p) {
return;
}
},
update: function( event, ui ) {
console.log($(".sortable").sortable("toArray", {attribute: 'data-id'}));
saveSortable($(".sortable").sortable("toArray", {attribute: 'data-id'}));
}
});
$( '.sortable' ).disableSelection();
$(".nc_lezione_pubblicata").change(function() {
if( $(this).is(':checked') ){
$( '<li class="nc_lezione_del_corso" data-id="'+$(this).attr('id')+'" id="lezione_del_corso_'+$(this).attr('id')+'"><code class="hndle"> -[]- </code>'+$(this).data('title')+'</p>' ).appendTo( ".sortable" );
}else{
$("#lezione_del_corso_"+$(this).attr('id')).remove();
console.log("elimino");
}
saveSortable($(".sortable").sortable("toArray", {attribute: 'data-id'}));
});
});
</script>
<ul class="sortable ui-sortable">
<?php foreach ($array_lezioni_del_corso[0] as $lezione_del_corso) { ?>
<li class="nc_lezione_del_corso" id="lezione_del_corso_<?php echo $lezione_del_corso; ?>"
data-id="<?php echo $lezione_del_corso; ?>">
<code class='hndle'> -[]- </code>
<?php echo get_the_title($lezione_del_corso); ?>
</li>
} ?>
</ul>
<p>
<script type="text/javascript">
$(function(){
console.log($(".sortable").sortable("toArray"));
});</script>
</p>
<?php }
function nc_savePortable_lezioni(){
global $wpdb;
$post_id = $_POST['post_id'];
$array_lezioni_del_corso = $_POST['array_lezioni_del_corso'];
update_post_meta($post_id, 'array_lezioni_del_corso', $array_lezioni_del_corso);
foreach ($array_lezioni_del_corso as $lezione_del_corso) {
$corsi_associati = get_post_meta($lezione_del_corso, 'corsi_associati', true);
$corsi_associati = $corsi_associati.','.$post_id;
update_post_meta(parseInt($lezione_del_corso), 'corsi_associati', $corsi_associati);
}
echo 'al server arrriva '.$array_lezioni_del_corso;
}
function nc_savePortable_lezioni_js(){
?>
<script type="text/javascript">
function saveSortable(sortableArraystringData){
var data = {
action: 'nc_savePortable_lezioni_action',
array_lezioni_del_corso : sortableArraystringData,
post_id : <? echo get_the_ID(); ?>
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
alert('This was passed ' + sortableArraystringData);
});
}
</script>
<?php
}
You're missing a curly brace somewhere.
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')); ?>);