• 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

<?php
/*
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

<?php
/*
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">
    <?php screen_icon(); ?>
    <h2>Insert JavaScript</h2>
 
    <?php
    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>
    <?php
}
 
// Draw users page
function ssd_insertjs_users_page() {
    ?>
    <div class="wrap">
    <?php 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>
    <?php
}
 
//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

<?php
/*
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

#106
seo says:
Tuesday, September 23, 2014 8:43 PM
Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword....wait there's even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at. <a href="http://www.2014PandaSeo.com">Seo Plugin</a>
#15
Tuesday, August 26, 2014 8:33 AM
It is a very adaptable process. You may get balloon printing of one's preferred shades and type. In reality, there are usually many printing selections for you.
[url=http://madepaintings.tumblr.com]Oil Paintings Reproductions[/url]
#5
Friday, August 22, 2014 2:55 AM
How extensive are my outlines? *faints at the thought* I’ll go with practically nonexistent. I’m one of those writers where something as simple as a three page synopsis shuts my creativity down to the point I put it away and never want to touch the project again.

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.