//the homepage
$(document).ready(function(){
myplugin_cast_vote(20);
});
//the plugin
<?php
add_action('wp_head', 'myplugin_js_header' );
function myplugin_js_header() // this is a PHP function
{
// use JavaScript SACK library for Ajax
wp_print_scripts( array( 'sack' ));
// Define custom JavaScript function
?>
<script type="text/javascript">
//<![CDATA[
function myplugin_cast_vote(posts)
{
$.post("<?php bloginfo( 'wpurl' ); ?>/wp-content/themes/fullscreen/function.php",
{
action : "process_thumbs" ,
numposts : posts,
results_div_id : output
});
} // end of JavaScript function myplugin_cast_vote
//]]>
</script>
<?php
add_action('wp_ajax_process_thumbs', 'my_action_callback');
add_action('wp_ajax_nopriv_process_thumbs', 'my_action_callback');
} // end of PHP function myplugin_js_header
?>
//functions.php
function my_action_callback(){
alert('sdfsdf');
$numposts = $_POST['numposts'];
$results_id = $_POST['results_div_id'];
die( "document.getElementById('$results_id').innerHTML = '$numposts'" );
}
I might not even sure my code is in the right place as I didnt really understand wordpress's example.
The homepage executes the plugin function that makes the ajax call. I add the actions and direct it to the callback function in functions.php. Of course at that point the callback isn't being run.
Any ideas?
You should post data to site_url('/wp-admin/admin-ajax.php') instead of your function.php
Related
I am creating a custom plugin in WordPress admin and I want to use ajax call inside plugin. My plugin has different folders. I have written following scripts but its giving me 400 (bad request) error. I use latest wordpress.
I tried to get answer from other post but not able to find what is the issue here. When I am moving the code to function.php everything working but I dont want to use function.php.
The code below has been written in single php file ( say dahboard.php)
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: ajaxurl, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
Try this. I've changed "ajaxurl" to <?php echo admin_url( 'admin-ajax.php' ); ?>
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: <?php echo admin_url( 'admin-ajax.php' ); ?>, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
I am using WPForms plugin with a Multipage Form. But the thing is When I click NEXT on an empty required field the page scrolls down I don't know why. How to disable scrolling effect. Please help
Link: https://platinumcapitalconnect.com/
You may try as below:
function wpf_disable_multipage_scroll() {
?>
<script type="text/javascript">window.wpforms_pageScroll = false;</script>
<?php
}
add_action( 'wpforms_wp_footer', 'wpf_disable_multipage_scroll' );
// Disable scrolling on error and submissions.
function wpf_dev_disable_scroll_effect_on_all_forms( $forms ) {
foreach ( $forms as $form ) {
?>
<script type="text/javascript">
wpforms.scrollToError = function(){};
wpforms.animateScrollTop = function(){};
</script>
<?php
}
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_disable_scroll_effect_on_all_forms', 10, 1 );`enter code here`
Please consider the code:
function mcqac_wp_enqueue_assets() {
if (is_admin()) {
wp_enqueue_script(
'mcqac-js-admin', // Handle.
PLUGIN_URL . 'build/main-admin.js',
array( 'jquery' ), // Dependencies, defined above.
filemtime( PLUGIN_PATH . 'build/main-admin.js' ), // Version: File modification time.
true // Enqueue the script in the footer.
);
$mcqacAdminData = array();
if (get_the_ID()) {
$mcqacAdminData['options'] = get_post_meta(get_the_ID(), 'mcqac_options', true);
}
wp_localize_script('mcqac-js-admin', 'mcqacAdminData', $mcqacAdminData);
}
}
add_action('init', 'mcqac_wp_enqueue_assets');
The get_the_ID() does not return anything when I am in the edit post page. Seems like init action hook is fired before the post query.
What is the solution?
Problem fixed the admin_enqueue_scripts action hook instead of init action hook. And also declare global $post to get post id from this variable.
Try the template_redirect hook instead of init.
add_action('template_redirect', 'prefix_get_page_id');
I know that by default WordLift publishes the JSON-LD asynchronously after the page load with an AJAX request and then injects the JSON-LD in the page head.
However I prefer to have the JSON-LD loaded by WordPress synchronously and avoid the AJAX call, how do I do that?
In order to turn WordLift's JSON-LD to asynchronous, you can add the following to the theme's functions.php file:
// Disable the asynchronous JSON-LD.
add_filter( 'wl_jsonld_enabled', function () {
return false;
} , 10, 0 );
// Hook to the `wp_head` to output the JSON-LD.
add_action( 'wp_head', function () {
// Check that the Wordlift_Jsonld_Service exists.
if ( ! class_exists( 'Wordlift_Jsonld_Service' ) ) {
echo '<!-- WordLift JSON-LD service not found. -->';
return;
}
// Determine whether this is the home page or whether we're displaying a single post.
$is_homepage = is_home() || is_front_page();
$post_id = is_singular() ? get_the_ID() : null;
// Get the JSON-LD.
$jsonld = json_encode( Wordlift_Jsonld_Service::get_instance()
->get_jsonld( $is_homepage, $post_id ) );
// Finally print the JSON-LD out.
?>
<script type="application/ld+json"><?php echo $jsonld; ?></script>
<?php
}, 10, 0 );
I m using WP pointers in my code...Pointers not display again after i dismiss it once... i deleted my plugin and again activate it but wp pointers not display again... If i install a new wordpress then WP Pointers display but when i dismiss it, then it never comes agains.. Is there a way that when plugin is activated wp pointers appears again...?? Here is my code
function thsp_enqueue_pointer_script_style( $hook_suffix ) {
// Assume pointer shouldn't be shown
$enqueue_pointer_script_style = false;
// Get array list of dismissed pointers for current user and convert it to array
$dismissed_pointers = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
// Check if our pointer is not among dismissed ones
if( !in_array( 'thsp_pointer', $dismissed_pointers ) ) {
$enqueue_pointer_script_style = true;
// Add footer scripts using callback function
add_action( 'admin_print_footer_scripts', 'thsp_pointer_print_scripts' );
}
// Enqueue pointer CSS and JS files, if needed
if( $enqueue_pointer_script_style ) {
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
}}add_action( 'admin_enqueue_scripts', 'thsp_enqueue_pointer_script_style' );
function thsp_pointer_print_scripts() {
$pointer_content = "<h3>My New Plugin</h3>";
$pointer_content .= "<p>If you ever activated a plugin, then had no idea where its settings page is, raise your hand.</p>";
?>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
$('#toplevel_page_settings').pointer({
content:'<?php echo $pointer_content; ?>',
position:{
edge: 'left', // arrow direction
align: 'center' // vertical alignment
},
pointerWidth: 350,
close:function() {
$.post( ajaxurl, {
pointer: 'thsp_pointer', // pointer ID
action: 'dismiss-wp-pointer'
});
}
}).pointer('open');
});
//]]>
</script>
Dismissed pointers are stored in the wp_usermeta table, for each user, sub meta key dismissed_wp_pointers. Ideally, your plugin should remove the IDs of your pointers for each user on deactivation (see register_deactivation_hook), if you want them to reappear on reactivation.