Using php inside javascript in wordpress - php

I have got the following code in a javascript(jquery) file called custom.js:
blabla = new Date(2014, 06 - 1, 2);
$('.days').countdown({
until: blabla,
layout: '{dn} {dl}',
1.Now i want a user to be able to change the above date. I have created a theme options page called theme-options.php
2.I am using <?php require_once('theme-options.php'); ?> in the functions.php to link to theme-options.php.
3.This is theme-options.php:
<?php
add_action('admin_menu', 'director_create_menu');
function director_create_menu() {
add_submenu_page( 'themes.php', ' Theme Options',
'Theme Options', 'administrator', __FILE__,
'director_settings_page');
add_action( 'admin_init', 'director_register_settings' );
}
function director_register_settings() {
register_setting( 'director-settings-group', 'director_date' );
}
div class="wrap">
<h2>Theme Settings</h2>
<form id="landingOptions" method="post" action="options.php">
<?php settings_fields( 'director-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Date:</th>
<td>
<input type="text" placeholder="2014, 06 - 1, 2" name="director_date"
value="<?php print get_option('director_date');
?>" />
</td>
</tr>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php } ?>
Basically what is happening is that there is a theme options page.A user gives a date inside it. Now i want to use that date inside the javascript file.
If I had to use it inside index.php it would've been
<?php $date = get_option('director_date'); ?>
<?php if( $date ) : ?> <?php echo $date; ?><?php endif; ?>);
. However this is javascript. How do i implement such an action here?
An alternative path but still doesn't work.Rename custom.js to custom.php and add:
gapinvite = new Date(<?php $date = get_option('director_date'); ?>
<?php if( $date ) : ?> <?php echo $date; ?><?php endif; ?>);
$('.days').countdown({
until: gapinvite,
layout: '{dn} {dl}',
And in functions.php:
<?php require_once('js/custom.php'); ?>

Take a look at wp_localize_script. Although ostensibly for translation, it lets you pass PHP values to JS. Here's a fairly good walkthrough on how to use it.
EDIT:
First, ensure that you are loading your JS using wp_enqueue_script, as you will need to refer to the handle. In your functions.php you'd have:
$date = get_option('director_date');
wp_enqueue_script('my-script', get_stylesheet_directory_uri() . 'js/my-script.js');
wp_localize_script('my-script', 'my_script_vars', array(
'date' => $date
)
);
wp_localize_script takes three arguments:
The handle of he script to pass the variables object to. This should match the handle being used in wp_enqueue_script
The name of the variable object you want to create. You will use this name to refer to the object in JS
The array of variable to pass to the object. Here I'm just passing the $date variable we declared earlier, but you can obviously pass whatever you want.
Then in my-script.js, you'd access this object very simply:
var $date = my_scripts_vars.date;
alert($date); // Or do whatever you want with it

In index.php, you can do something like:
<?php $date = get_option('director_date'); ?>
<script type="text/javascript">var generated_date = '<?php echo $date; ?>';</script>
Just make sure you do this before you include the JavaScript where you want to reference the generated_date variable you just created.

Related

Plugin Page showing Warning: call_user_func_array() expects parameter 1

I am making a plugin and when i add a page for managing_option the page is showing me
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘myplugin_options_page’ not found or invalid function name in D:\xampp\htdocs\connect\wp-includes\class-wp-hook.php on line 286
Please find below the code i am using for the plugin
Main Page
<?php
/*
Plugin Name: Name
URI: https://example.com/
Description: abc
Author: Pratik Purohit
License: GPLv2 or later
*/
function myplugin_register_settings() {
add_option( ‘myplugin_option_name’, ‘This is my option value.’);
register_setting( ‘myplugin_options_group’, ‘myplugin_option_name’, ‘myplugin_callback’ );
}
add_action( ‘admin_init’, ‘myplugin_register_settings’ );
function myplugin_register_options_page() {
add_options_page(‘Page Title’, ‘Plugin Menu’, ‘manage_options’, ‘myplugin’, ‘myplugin_options_page’);
}
add_action(‘admin_menu’, ‘myplugin_register_options_page’);
Managing page
<?php function myplugin_options_page()
{
?>
<div>
<?php screen_icon(); ?>
<h2>My Plugin Page Title</h2>
<form method=”post” action=”options.php”>
<?php settings_fields( ‘myplugin_options_group’ ); ?>
<h3>This is my option</h3>
<p>Some text here.</p>
<table>
<tr valign=”top”>
<th scope=”row”><label for=”myplugin_option_name”>Label</label></th>
<td><input type=”text” id=”myplugin_option_name” name=”myplugin_option_name” value=”<?php echo get_option(‘myplugin_option_name’); ?>” /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
} ?>
You need to put your myplugin_options_page function before this line..
add_options_page(‘Page Title’, ‘Plugin Menu’, ‘manage_options’, ‘myplugin’, ‘myplugin_options_page’);
.. because Wordpress is trying to callback your function using the call_user_func_array PHP function, and throws an error because the function has not been found.
If you've defined the myplugin_options_page function in another file, you should add a require (doc) to your main page in order to include your function before the execution of the add_options_page().

Yii: from a view, how do I found out where a variable came from?

In a Yii project passed to me, there's a function that creates (or shows?) a textbox when the button/link Comment is clicked. From there, the user can create Comments, which will be displayed in a row.
I'm trying to see if I can create an edit comment function, so I thought I could go about this by copying the Comment function - it'll show a textbox, and the user can input the new text in there. And instead of adding a new comment, it will edit the existing one.
But I hit a snag, as apparently the view.php makes use of a variable that I couldn't for the life of me figure out how to use in _comments.php - the file responsible for displaying the individual comments, afaik.
Here's the code for view.php:
</script>
<?php
$this->breadcrumbs=array('Forums'=>array('index'),$model->title,);
?>
<?php
if(Yii::app()->user->hasFlash('message')):
echo '<script>alert("'.Yii::app()->user->getFlash('message').'");</script>';
endif;
?>
<?php $starter = Persons::model()->findByAttributes(array('party_id'=>$model->party_id));?>
<div id="forum_main_box">
<div class="comment-icon-textbox">
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
</div>
<?php endif; ?>
<div id="forum_comment_headerbox">
</div>
<div>
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</div>
</div>
Of that code, below is the Comment link I spoke of:
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
<?php } ?>
This block displays the list of comments, and what (I assume to be) the space where the textbox will pop up when the above Comment is clicked:
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
Notice that both makes use of $model. It first appeared in the code as $model->title.
And here's a shortened version of the _comments.php, which is used for the comment rows and the comment box.
<?php $comment = $data; ?>
<div class="other-member-comment-box">
<?php $person=Persons::model()->findByAttributes(array('party_id'=>$comment->party_id)); ?>
<?php
$country=Lookup_codes::model()->findByAttributes(array('id'=>$person->country));
$location = empty($country) ? '' : ' - '.$country->name;
// $model->title;
?>
<?php if (Yii::app()->user->id == $person->party_id || Yii::app()->partyroles->isAdmin()) {
?>
<p class="admin-commands">
<?php echo CHtml::link(CHtml::encode('Edit'),array('forum/editcomment','reply'=>$data->id,'topic'=>$data->content_id)); ?>
<?php echo CHtml::ajaxLink('EditTestComment',array('forum/callcommentform'),array('update' => '#render_div'.$model->id,'data'=>array('id'=>$model->content_id),)); ?>
<?php echo CHtml::link(CHtml::encode('Delete'),array('forum/delete','reply'=>$data->id,'topic'=>$data->content_id),array('confirm'=>'Are you sure you want to delete this item?')); ?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</p>
<?php } ?>
</div>
Under <p class="admin-commands">, there's a EditTestComment link which is a straight up copy of the Comment code from the view.php. This doesn't work, of course, because of this:
2016/04/07 10:24:03 [error] [php] Undefined variable: model
Where'd $model come from in view.php? Because putting in the same line ($model->title) anywhere in _comments.php just breaks it further.
EDIT: Here's the CallComment part of the controller:
public function actionCallCommentForm($id='')
{
$topic=Forum::model()->findByPk($id);
$this->renderPartial('_commentform', array(
'forum'=>$topic,
'model'=>new Comment,
//'view'=>array('view','id'=>$id),
'view'=>'view',
));
}
$model variable is coming from your controller initially. It is an instance of a Comment class which gets passed to the view via $this->render('view', array('model'=>$whatever)). This line will make $whatever available in the forum/view.php file under the name of $model. Now since you are dealing with partial views you have to track it because it is possible that the same $model variable will be passed to another partial view with $this->renderPartial('_comment', array('whatever'=>$model)) and now this will be accessible in partial view as $whatever.

Update notification is not showing

I am developing a gallery plugin. So i created a custom post type and under it i created a submenu page for changing gallery options such a border-width, border-color, border-radius etc. when i update the values, all are working but update notification is not showing.
I am writing main code here. Please let me know what is problem.
function gallery_setting_pages() {
add_submenu_page(
'edit.php?post_type=gallary',
'Gallery Settings',
'Settings',
'manage_options',
'gallery-setting',
'gallery_option_page_functions'
);
}
add_action('admin_menu','gallery_setting_pages');
if ( is_admin() ) :
function ppmscrollbar_register_settings() {
register_setting('gallery_plugin_options','rt_gallery_options','rt_gallery_validate_options');
}
add_action('admin_init','ppmscrollbar_register_settings');
function gallery_option_page_functions() {
global $rt_gallery_options;
if ( ! isset ($_REQUEST['updated'] ) )
$_REQUEST['updated']=false;
?>
<?php if ( false !==$_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e('options saved'); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this show the notification ?>
<form method="post" action="options.php">
<?php $settings=get_option( 'rt_gallery_options', $rt_gallery_options); ?>
<?php settings_fields('gallery_plugin_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="cursor_color">Select Gallery Border Color</label></th>
<td>
<input id="cursor_color" type="text" name="rt_gallery_options[cursor_color]" value="<?php echo stripslashes($settings['cursor_color']); ?>" class="my-color-field" /><p class="description">In a few words, explain what is this.</p>
</td>
</tr>
</table>
<p class="submit"><input type="submit" value="Save Changes" class="button button-primary" id="submit" name="submit"></p>
</form>
</div>
<?php
}
function rt_gallery_validate_options( $input ){
global $rt_gallery_options;
$settings = get_option( 'rt_gallery_options', $rt_gallery_options );
//We strip all tags from the text field, to avoid vulnerablilties like XSS
$input['cursor_color']=wp_filter_post_kses( $input['cursor_color']);
return $input;
}
endif; //EndIf is_admin()
thanks for being posted the question on stackoverflow to get answers. We are here to help you.Please mention the page name you are using above , and let me know where you are updating the variable "updated" in your page. The issue might be, if you are not setting the variable, then if condition always fails, and notification will not be shown.

how to post the data with out click on checkbox in cakephp?

I have form where the select boxes are there and select all option, i want to post all the data without click on check boxes, i dont want to view this page, mean if user go to signup page the data will already selected by default how can i do that wothout any view?
this is my view code
<?php v_start($this);?>
<?php
#if(element exits of controller-action)
echo $this->element('validations/users/signup');
?>
<script>
function toggleChecked(status)
{
jQuery(".new-checkbox input").each( function() {
jQuery(this).attr("checked",status);
}
)
}
</script>
<h1><span style="color:CornflowerBlue"><?php echo __l('Step1');?></span></h1>
<?php
$form = $this->FormManager;
echo $form->create('User',array_merge($form_options,array('default'=>true)));
?>
<table width = "100%">
<tbody>
<tr>
<td>
<?php echo $form->input('',array('id'=>'mark-all','onclick'=>'toggleChecked(this.checked)','type'=>'checkbox','label'=>'Select All Products' ));?>
<?php echo $form->input('product_id',
array(
'multiple'=>'checkbox',
'options'=>$products,
'div'=>false,
'class'=>'new-checkbox'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo $this->FormManager->end(LBL_BTN_NEXT);
?>
</td>
</tr>
</tbody>
</table>
i dont want this page, the user will select the data by default.
this is my controller code
$products = $this
->User
->ProductsUser
->Product
->find('list',array(
'fields' => 'product_id,name',
'conditions'=>array(
'Product.is_visible'=>true
)
)
);
$this->set('products',$products);
if($this->request->is('post'))
{
$this->Session->write('signUp.signUpProduct',$this->request->data['User']);
$this->redirect(array('action'=>'signup_product'));
}
}
how can i do that, Thanks in advance.
The attr() doesn't take true or false as an argument.
jQuery(this).attr("checked","checked"); // Correct usage of attr()
Although I think attr is deprecated for the favorable prop() method.
jQuery(this).prop("checked",status);
Which indeed does take a true/fales argument. :)
Hope this helps.

add php inside the short code

Well what I am trying to do is add a barcode to my wordpress sidebar. The plugin used to generate the barcode is called Yeblon
Yeblon Plugin Page
the shortcode used by the plugin is
[yeblonqrcode size="100" url="" class="" style=""]
were url is the place were the generated barcode leads to
The url I want to generate is inserted from the custom fields. I use a plugin called Advanced Custom Fields.
Advanced Custom Fields Plugin Page
the code that displays the link is
<?php the_field("download_(android)" , $post->ID); ?>
So my final code is
<div id="mobile-barcodes-tabs">
<?php $post = $wp_query->post; ?>
<?php
if(get_field('download_(android)')){ ?>
<?php echo do_shortcode('[yeblonqrcode size="100" url="the_field("download_(android)" , $post->ID);" class="" style=""] ');?>
<?php }
?>
</div>
But it is not working I don't know what is the problem I will be glad if you helped me thanks in advance
Build the PHP values beforehand and use string concatenation:
<div id="mobile-barcodes-tabs">
<?php
$post = $wp_query->post;
$the_url = get_field( 'download_(android)' , $post->ID );
if( $the_url ) {
echo do_shortcode( '[yeblonqrcode size="100" url="' . $the_url . '" class="" style=""]' );
}
?>
</div>
PS: You probably noticed that I removed all those unnecessary opening and closing PHP tags.

Categories