Datetime Picker Wordpress Plugin - wordpress

Below is a page code of my custom plugin where I am trying to use jquery datetime picker.
The picker script was taken from this link. But due to some reason I am getting an Error message saying "jQuery("#calendar1, #calendar2").calendar is not a function". I have checked that the files required are included correctly and before the custom code. But still there is this error.
<?php
function list_my_styles() { // ADDING THE SCRIPTS
$plugin_url = WP_PLUGIN_URL.'/my_custom_datepicker/';
wp_register_style('my-addon-datepicker-css',$plugin_url.'jquery-calendar.css');
wp_enqueue_style('my-addon-datepicker-css');
wp_enqueue_script('jquery');
wp_register_script('calendar_plugin',$plugin_url.'jquery-calendar.js');
wp_enqueue_script('calendar_plugin');
}
add_action('wp_enqueue_scripts','list_my_styles');
function my_custom_datetime_script() { // ADDING THE JQUERY SCRIPT
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#calendar1, #calendar2").calendar();
jQuery("#calendar1_alert").click(function(){alert(popUpCal.parseDate(jQuery('#calendar1').val()))});
});
</script>
<?php
}
add_action('wp_head','my_custom_datetime_script');
function custom_datetime_picker_area() {
?>
<input type="text" id="calendar1" class="calendarFocus"/>
<input type="button" id="calendar1_alert" value="Alert datetime object"/>
<?php
}
add_shortcode('my_datepicker','custom_datetime_picker_area');
?>
Any Help will be appreciated
Thanks

This is what I use in my plugin:
/* add jquery ui datepicker and theme */
global $wp_scripts;
wp_enqueue_script('jquery-ui-datepicker');
$ui = $wp_scripts->query('jquery-ui-core');
$url = "https://ajax.aspnetcdn.com/ajax/jquery.ui/{$ui->ver}/themes/redmond/jquery.ui.all.css";
wp_enqueue_style('jquery-ui-redmond', $url, false, $ui->ver);
The CDN I'm using has all of the available themes to choose from as well.
Then on my form page i use:
<label for="date_of" style="width: 135px !important;">Presentation Date
<?php echo '<img src="'.$this->pluginurl.'images/help.png"
title="Enter the Presentation date here." />';?>
</label>
<input type="text" id="date_of" name="date_of" value="" />
and in:
jQuery(document).ready(function() {
jQuery('#date_of').datepicker({
dateFormat: 'yy-mm-dd'
});
});
I load plugin.js file with register/enqueue_script which contains the document.ready function.
Hope this helps.

The error means it can not find this function , which in turns means either :
1 - it can not find the script , or
2 - you are trying to execute the script before it is loaded ..
One thing I did not understand - where is this code going in your plugin ??
Anyhow, first verify that you have a wp_head() and wp_head() action (template tags) in your THEME where you test the plugin, Then , also try to move the action to the footer ...
function my_custom_datetime_script() { // ADDING THE JQUERY SCRIPT
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#calendar1, #calendar2").calendar();
jQuery("#calendar1_alert").click(function(){alert(popUpCal.parseDate(jQuery('#calendar1').val()))});
});
</script>
<?php
}
add_action('wp_footer','my_custom_datetime_script');

Related

in_category method in wordpress and script tags - AdjacentHTML

I am trying to show .gif banner on a posts only within specific category after first paragraph in wordpress, so I my code in loop-single.php looks like:
<?php if(in_category('my_category')){ ?>
<script>
const par = document.querySelector('p');
par.insertAdjacentHTML('afterend', '<div><img src="some_gif" alt="" class=""></div>')
</script>
<?php } ?>
But it doesn't work as expected. Anyone have an idea? Or may be some other solution that not recquires installing additional plugins (not that I don't want to, most of them just slow the site down).
Thank you
edit: 'after first paragraph' added
So, I found a way, firstly I needed to check if the post is in wanted category, than create a function and call it afterwards. The code is set in single-loop.php file in my template:
<?php if(in_category('my-category')){ ?>
<script type="text/javascript">
function krasAd() {
const par = document.querySelector("p");
par.insertAdjacentHTML('afterend', '<div><img src="my-gif" alt="" class=""></div>')
};
krasAd();
</script>
<?php } ?>
There was also one way to check if you have a string in URL that only recquires JS. But it recquires you to have properly created URL structure, for example: domain.com/category/post
If I had category in my posts URL I would use code below, the code is set before closing footer tag in my single-loop.php template file:
<script type="text/javascript">
if (window.location.href.indexOf("my-string") > -1) {
const par = document.querySelector("p");
par.insertAdjacentHTML('afterend', '<div><img src="my-gif" alt="" class=""></div>')
}
</script>

How to add radio button to my website in wordpress

I am using WordPress for my website.
I want to add radio buttons to my WordPress page when a radio button is selected I want to display a table below. Can anybody help me
Hello maybe more details help ppl to tell you better answer. but with
this much of information i'v got something for you : this is just
sample. you just need to edit for your specific purpose.
** put html code in wordpress editor ( put in editor section, not visual, visual editor maybe auto convert some tags)
** put js codes into your js script tag some where!! if you don't have any idea just use this plugin
** if jQuery not worked add this lines to function.php of your theme or child-theme to add jQuery library to your page(99% wordpress load it by default) :
// include custom jQuery
function shapeSpace_include_custom_jquery() {
wp_deregister_script('jquery');
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'shapeSpace_include_custom_jquery');
HTML CODE :
<form>
<div>
<input type="radio" name="fruit" value="orange" id="orange">
<label for="orange">orange</label>
</div>
<div>
<input type="radio" name="fruit" value="apple" id="apple">
<label for="apple">apple</label>
</div>
<div>
<input type="radio" name="fruit" value="banana" id="banana">
<label for="banana">banana</label>
</div>
<div id="show-table"></div>
</form>
SCRIPT CODE:
<script>
jQuery(document).ready(function ($) {
$("input").on("click", function () {
var checkedNode = $("input:checked").val();
var orangTable = '<table style="width:100%"><tr><th>fruit</th></tr><tr><td>name</td></tr><tr><td>orang</td></tr></table>';
var appleTable = '<table style="width:100%"><tr><th>fruit</th></tr><tr><td>name</td></tr><tr><td>apple</td></tr></table>';
var bananaTable = '<table style="width:100%"><tr><th>fruit</th></tr><tr><td>name</td></tr><tr><td>banana</td></tr></table>';
switch (checkedNode) {
case 'orange':
$("#show-table").html(orangTable);
break;
case 'apple':
$("#show-table").html(appleTable);
break;
case 'banana':
$("#show-table").html(bananaTable);
break;
default:
}
}
}
</script>
specific table assigned to each button will display inside div
with id="show-table" when that button selected. you can outspread this
method for what purpose you have. just know this is not efficient
method or best way. it's just a way ..

Activate link upon reCAPTCHA success?

I'm working on a WordPress site. Basically, I publish download links (PDFs), and would like to stop web crawlers from accessing this content. This led me to Google's reCAPTCHA. Can I use this alone, so that when a user clicks/answers correctly, the links on the page will be made active? I'm having trouble editing a page in WordPress to do this. Thanks.
-Rudy.
I understand that you want to show link dynamically after the verification of the recaptcha.
You can create a an ajax that fetches the link after the verifcation of recaptcha passes.
To do this, we will use WordPress ajax requests, wp-ajax:
First, you register the ajax handler the request in the server side
add_action( 'wp_ajax_get_hidden_pdf_link', 'search_hidden_pdf_link' );
// add this line to handle requests of non logged in users
add_action( 'wp_ajax_nopriv_get_hidden_pdf_link', 'search_hidden_pdf_link' );
function search_hidden_pdf_link() {
// the response will be ajax response
header('Content-Type: application/json;charset=utf-8');
if(recaptcha_fails()){
// writing the failure response
echo json_encode(array('object' => 'error'));
wp_die();
}
$secret_pdf_link = generate_pdf_link();
// writing the succcess response
echo( json_encode( array('object' => 'success', 'link' => $secret_pdf_link)));
wp_die();
}
and in the front end, you create an ajax form, which asks and displays the link.
PDF Link
<form id="pdf-link-form" action="<?php echo admin_url('wp-ajax.php'); ?>">
<!-- some input that tells the backend which pdf to fetch -->
<input type="hidden" name="public_pdf_id" value="<?php echo $pdf_id; ?>">
<!-- the ajax request identifier, it is the suffix inside the action -->
<input type="hidden" name="action" value="get_hidden_pdf_link">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
</form>
<script>
$(document).ready(function () {
$('#pdf-link-form').submit(function (event) {
event.preventDefault();
form = $(this);
$.ajax({
type: 'POST',
url: form.attr('action'),
data: form.serializeArray()
}).done(function (result) {
if(result.object == 'success'){
$('#hidden-pdf-link').attr('href', result.link);
form.remove();
alert('you can access the pdf')
} else {
alert('you are not allowed to access my pdf!!');
}
})
});
});
</script>

I would like to diplay the output(Mickey) in a wordpress page, instead of php page

The following code is in a wordpress page.
<form action="action_page.php" method="post">
Name:<br>
<input type="text" name="name" value="Mickey">
<input type="submit" value="Submit">
</form>
action_page.php
<?php $name=$_POST['name']; echo $name;?>
I would like to diplay the output(Mickey) in a wordpress page, instead of php page. Please help me.
You can achieve it via AJAX, see the codex
Add this to functions.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
function prefix_ajax_add_foobar() {
// Handle request then generate response using WP_Ajax_Response
// In a real world scenario, apply all the validation,
// sanitization and black magic to POSTed variables before using them
$name=$_POST['name'];
echo $name;
}
Now the following could go in footer.php inside of <script> tags or in a separate JS file.
jQuery.post(
ajaxurl,
{
'action': 'add_foobar',
'data': jQuery("form").sanitize()
},
function(response){
alert('The server responded: ' + response);
}
);
All things being equal you should see an alert with the value that was posted.

How can I pass information to an iframe via Post in ASP.NET?

I would like to pass information to an iframe via post. (Could be jquery or javascript that executes the post, it doesn't really matter).
The information cannot be sent via querystring as I do not have access to change the way the page brought in by the iframe is.
This data will determine the layout of the content in the iframe so how can I make it so that after the post is sent the iframe is updated? (possibly refresh?)
I wrote a blog post about doing this with jQuery to upload a file using a hidden iframe. Here's the code:
Here is the HTML for the form:
<div id="uploadform">
<form id="theuploadform">
<input type="hidden" id="max" name="MAX_FILE_SIZE" value="5000000" >
<input id="userfile" name="userfile" size="50" type="file">
<input id="formsubmit" type="submit" value="Send File" >
</form>
The DIV in which to allow jQuery to create the iframe you can hide it with a little CSS:
<div id="iframe" style="width:0px height:0px visibility:none">
</div>
The DIV in which to show the results of the callback:
<div id="textarea">
</div>
The jQuery code:
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#formsubmit").click(function() {
var userFile = $('form#userfile').val();
var max = $('form#max').val();
var iframe = $( '<iframe name="postframe" id="postframe" class="hidden" src="about:none" />' );
$('div#iframe').append( iframe );
$('#theuploadform').attr( "action", "uploader.php" )
$('#theuploadform').attr( "method", "post" )
$('#theuploadform').attr( "userfile", userFile )
$('#theuploadform').attr( "MAX_FILE_SIZE", max )
$('#theuploadform').attr( "enctype", "multipart/form-data" )
$('#theuploadform').attr( "encoding", "multipart/form-data" )
$('#theuploadform').attr( "target", "postframe" )
$('#theuploadform').submit();
//need to get contents of the iframe
$("#postframe").load(
function(){
iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
$("div#textarea").html(iframeContents);
}
);
return false;
});
});
</script>
I used a php app like this uploader.php to do something with the file:
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$maxfilesize = $_POST[MAX_FILE_SIZE];
if ($maxfilesize > 5000000) {
//Halt!
echo "Upload error: File may be to large.<br/>";
exit();
}else{
// Let it go
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print('File is valid, and was successfully uploaded. ');
} else {
echo "Upload error: File may be to large.<br/>";
}
chmod($uploadfile, 0744);
?>
There's more there than you need, but it illustrates the concept in jQuery.
I don't have the code handy but my team accomplished this purely in Javascript. As I recall it went something like this:
function postToPage() {
var iframe = document.getElementById('myIFrame');
if (iframe) {
var newForm = '<html><head></head><body><form...> <input type="hidden" name="..." value="..." /> </form><script type=\"text/javascript\">document.forms[0].submit();</scrip' + 't></body></html>';
iframe.document.write(newForm); //maybe wrong, find the iframe's document and write to it
}
}

Resources