Saturday, June 1, 2013

Most Frequent WordPrees actions and shortcode

To follow this tutorial you need to know WordPress basics about plugins and shortcode.
Just replace the Hello Dolly plugin with this code:


<?php
/**
 * @package Hello_Dolly
 * @version 1.6
 */
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/extend/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.6
Author URI: http://ma.tt/
*/

function after_closing_body_tag() {
 $c = 'after end body tag';
 echo "<div style='background-color:red;'>$c</div>";
}

function page_add ( $the_content ) {
 $before = "<h3>Before content from database</h3>";
 $after = ""; 
        if(is_single()){
       $after = "<h3>After content from database</h3>";
  }
 $new_content = $before.$the_content.$after;
 return $new_content;
}

function fs() {
 echo "<script>alert('from script in footer');</script>";
}

function head_script(){
 echo "<script>alert('from script in head tag');</script>";
} 
// Dont forget to embed shortcode in your post
function embed_shortcode( $atts ) {
 extract( shortcode_atts( array(
  'id' => ''
 ), $atts ) );

 $output = '<h3>' . $id . '</h3>';
 $output .= '<h3>' . get_permalink() . '</h3>';
 $output .= '<h3>' .  get_the_ID() . '</h3>'; 
 return $output;
}

add_action( 'wp_print_footer_scripts', 'fs' );

add_action('wp_head', 'head_script');

add_action( 'shutdown', 'after_closing_body_tag' );

add_shortcode( 'youtubevid', 'embed_shortcode' );

add_filter( 'the_content', 'page_add' );

?>
Note yellow highlighted portion of code. This is where we determine if post is alone on page or in archive. 

No comments:

Post a Comment