- No-Confl ict Mode in WordPress
- A Proper Way to Include Scripts
- Plugin Ajax Read More
- Plugin Front Delete Comment
No-Confl ict Mode in WordPress use one of these solutions: Write jQuery( ) instead of each $( ) || Use a jQuery wrapper.
//1 jQuery(‘.).each( function(){ jQuery(this).addClass( ‘); }); jQuery.data( document.body, ‘1337 ); // jQuery noConflict wrapper: (function($) { // $() will work here $(‘.).each( function(){ $(this).addClass( ‘); }); $.data( document.body, ‘1337 ); })(jQuery);
AJAX example: Read More
/* Plugin Name: Ajax Read More Plugin URI: http://example.com/ Description: Ajaxify the "Read more" links Version: 1.0 */ // Flag to state if the script is needed global $ssd_arm_needjs; $ssd_arm_needjs = false; // Plugin version, bump it up if you update the plugin define( 'ssd_ARM_VERSION', '1.0' ); // Enqueue the script, in the footer add_action( 'template_redirect', 'ssd_arm_add_js' ); function ssd_arm_add_js() { // Enqueue the script wp_enqueue_script( 'ssd_arm', plugin_dir_url( __FILE__ ).'js/script.js', array('jquery'), ssd_ARM_VERSION, true ); // Get current page protocol $protocol = isset( $_SERVER["HTTPS"]) ? 'https://' : 'http://'; // Output admin-ajax.php URL with same protocol as current page $params = array( 'ajaxurl' => admin_url( 'admin-ajax.php', $protocol ) ); wp_localize_script( 'ssd_arm', 'ssd_arm', $params ); } // Don't add the script if actually not needed add_action( 'wp_print_footer_scripts', 'ssd_arm_footer_maybe_remove', 1 ); function ssd_arm_footer_maybe_remove() { global $ssd_arm_needjs; if( !$ssd_arm_needjs ) { wp_deregister_script( 'ssd_arm' ); } } // Inspect each post to check if there's a "read more" tag add_action( 'the_post', 'ssd_arm_check_readmore' ); function ssd_arm_check_readmore( $post ) { if ( preg_match('/<!--more(.*?)?-->/', $post->post_content ) && !is_single() ) { global $ssd_arm_needjs; $ssd_arm_needjs = true; } } // Ajax handler add_action('wp_ajax_nopriv_ssd_arm_ajax', 'ssd_arm_ajax'); add_action('wp_ajax_ssd_arm_ajax', 'ssd_arm_ajax'); function ssd_arm_ajax() { // Modify the way WP gets post content add_filter( 'the_content', 'ssd_arm_get_2nd_half' ); // setup Query query_posts( 'p='.absint( $_REQUEST['post_id'] ) ); // "The Loop" if ( have_posts() ) : while ( have_posts() ) : the_post(); the_content(); endwhile; else: echo "post not found :/"; endif; // reset Query wp_reset_query(); die(); } // Get second part of a post after the "more" jump function ssd_arm_get_2nd_half( $content ) { $id = absint( $_REQUEST['post_id'] ); $content = preg_replace( "!^.*<span id=\"more-$id\"></span>!s", '', $content ); return $content; } //JS code if( typeof(console) == 'object' ) { console.log( 'script loaded' ); } (function($) { $('.more-link').click(function(){ var link = this; $(link).html('loading...'); // href="' . get_permalink() . "#more-{$post->ID}" var post_id = $(link).attr('href').replace(/^.*#more-/, ''); var data = { action: 'ssd_arm_ajax', post_id: post_id }; $.get(ssd_arm.ajaxurl, data, function(data){ $(link).after(data).remove(); }); return false; }); })(jQuery);
A Proper Way to Include Scripts
/* Plugin Name: Add JavaScript Plugin URI: http://example.com/ Description: Demonstrates how to properly insert JS into different pages */ // URL to the /js directory of the plugin define( 'ssd_INSERTJS', plugin_dir_url( __FILE__).'js' ); // Add new admin pages add_action('admin_menu', 'ssd_insertjs_add_page'); function ssd_insertjs_add_page() { // Add JS to all the admin pages wp_enqueue_script( 'ssd_insertjs_1', ssd_INSERTJS.'/script.js.php?where=admin' ); // Add a page under Settings $settings = add_options_page( 'Insert JS', 'Insert JS', 'manage_options', 'ssd_insertjs_settings', 'ssd_insertjs_options_page' ); // Add JS to the setting page add_action( 'load-'.$settings, 'ssd_insertjs_add_settings_script' ); // Add a page under Users $users = add_users_page( 'Insert JS', 'Insert JS', 'manage_options', 'ssd_insertjs_users', 'ssd_insertjs_users_page' ); // Add JS to the users page, with a different hook add_action( 'admin_print_scripts-'.$users, 'ssd_insertjs_add_users_script' ); } // Add JS to the plugin's settings page function ssd_insertjs_add_settings_script() { wp_enqueue_script( 'ssd_insertjs_2', ssd_INSERTJS.'/script.js.php?where=settings' ); } // Add JS to the plugin's users page, in the page footer function ssd_insertjs_add_users_script() { wp_enqueue_script( 'ssd_insertjs_3', ssd_INSERTJS.'/script.js.php?where=users', '', '', true ); } // Add JS to the Comments page add_action( 'load-edit-comments.php', 'ssd_insertjs_on_comments' ); function ssd_insertjs_on_comments() { wp_enqueue_script( 'ssd_insertjs_4', ssd_INSERTJS.'/script.js.php?where=comments' ); } // Add JS to pages of the blog add_action( 'template_redirect', 'ssd_insertjs_add_scripts_blog' ); function ssd_insertjs_add_scripts_blog() { // To all pages of the blog wp_enqueue_script( 'ssd_insertjs_5', ssd_INSERTJS.'/script.js.php?where=blog' ); // To single post pages if( is_single() ) { wp_enqueue_script( 'ssd_insertjs_6', ssd_INSERTJS.'/script.js.php?where=single' ); } // To the "About" page if( is_page('About') ) { wp_enqueue_script( 'ssd_insertjs_7', ssd_INSERTJS.'/script.js.php?where=about' ); } } // Draw options page function ssd_insertjs_options_page() { <div class="wrap"> screen_icon(); <h2>Insert JavaScript</h2> var_dump( plugin_basename( dirname(__FILE__) ) ); var_dump( plugin_dir_url( __FILE__) ); <p>This sample plugin selectively adds different JS to the following pages:</p> <ol> <li>Admin part: <ol> <li>This very page</li> <li>This plugin page under the Users menu</li> <li>The Comments page</li> </ol></li> <li>Blog part: <ol> <li>All blog pages</li> <li>Single post pages</li> </ol></li> </ol> </div> } // Draw users page function ssd_insertjs_users_page() { <div class="wrap"> screen_icon(); <h2>Insert JavaScript</h2> <p>This is another sample page. Here, the JS is added in the footer (view page source).</p> </div> } //code JS <?php // Send proper header for the browser header('Content-type: application/javascript'); // Get context to display appropriate notice $page = isset( $_GET['where'] ) ? $_GET['where'] : '' ; switch( $page ) { // admin pages: case 'admin': $context = "all admin pages"; break; case 'settings': $context = "plugin settings page"; break; case 'users': $context = "plugin users page"; break; case 'comments': $context = "comments page"; break; // public pages: case 'single': $context = "blog single pages"; break; case 'about': $context = "About page"; break; case 'blog': $context = "all blog pages"; break; } // Now javascript part, here a simple alert alert( 'This script should only load in: <?php echo $context; ?>' );
You now know that using wp_enqueue_script() you can elect to insert your script in the document header or at the end of the document body. In the Head - page elements may not be available to the script because they are not loaded yet, typically include libraries and function defi nitions that can then be used later in the page. Near the Footer - effective if you need to include a third - party script that can potentially slow down or halt the rendering of your page while it loads and executes. In the Page Content - do smth if if the post contains the appropriate shortcode. Inline
Frontend Comment Deletion
/* Plugin Name: Instant Delete Comment Plugin URI: http://example.com/ Description: Add a quick link to instantly delete comments */ // Add script on single post & pages with comments only, if user has edit rights add_action( 'template_redirect', 'ssd_idc_addjs_ifcomments' ); function ssd_idc_addjs_ifcomments() { if( is_single() && current_user_can( 'moderate_comments' ) ) { global $post; if( $post->comment_count ) { $path = plugin_dir_url( __FILE__ ); wp_enqueue_script( 'ssd_idc', $path.'js/script.js' ); $protocol = isset( $_SERVER["HTTPS"]) ? 'https://' : 'http://'; $params = array( 'ajaxurl' => admin_url( 'admin-ajax.php', $protocol ) ); wp_localize_script( 'ssd_idc', 'ssd_idc', $params ); } } } // Add an admin link to each comment add_filter( 'comment_text', 'ssd_idc_add_link' ); function ssd_idc_add_link( $text ) { // Get current comment ID global $comment; $comment_id = $comment->comment_ID; // Get link to admin page to trash comment, and add nonces to it $link = admin_url( 'comment.php?action=trash&c='.$comment_id ); $link = wp_nonce_url( $link, 'ssd_idc-delete-'.$comment_id ); $link = "<a href='$link' class='ssd_idc_link'>delete comment</a>"; // Append link to comment text return $text."<p>[admin: $link]</p>"; } // Ajax handler add_action( 'wp_ajax_ssd_idc_ajax_delete', 'ssd_idc_ajax_delete' ); function ssd_idc_ajax_delete() { $cid = absint( $_POST['cid'] ); $response = new WP_Ajax_Response; if( current_user_can( 'moderate_comments' ) && check_ajax_referer( 'ssd_idc-delete-'.$cid, 'nonce', false ) && wp_delete_comment( $cid ) ) { // Request successful $response->add( array( 'data' => 'success', 'supplemental' => array( 'cid' => $cid, 'message' => 'this comment has been deleted' ), ) ); } else { // Request failed $response->add( array( 'data' => 'error', 'supplemental' => array( 'cid' => $cid, 'message' => 'an error occured' ), ) ); } $response->send(); exit(); } //code JS - script.js jQuery(document).ready(function($) { $('.ssd_idc_link').click(function(){ var link = this; // get comment id and nonce var href = $(link).attr( 'href' ); var id = href.replace(/^.*c=(\d+).*$/, '$1'); var nonce = href.replace(/^.*_wpnonce=([a-z0-9]+).*$/, '$1'); var data = { action: 'ssd_idc_ajax_delete', cid: id, nonce: nonce } $.post( ssd_idc.ajaxurl, data, function(data){ var status = $(data).find('response_data').text(); var message = $(data).find('supplemental message').text(); if( status == 'success' ) { $(link).parent().after( '<p><b>'+message+'</b></p>' ).remove(); } else { alert( message ); } }); return false; }); });
3 comments
[url=http://madepaintings.tumblr.com]Oil Paintings Reproductions[/url]
Leave a Comment