Functions for WordPress

using wordpress shortcodes everywhere

The following codes go into your php and css files but mainly your functions.php file. If you are not sure what you are doing then contact me. The price for putting any of these codes into your functions theme is only $35 that is one or more, contact me if you want me to do so.  The location of the file is:

  1. dashboard
  2. appearance
  3. editor
  4. functions.php

Function in WordPress for users to add media

So if you want a contributor to your site to be able to add media just put this code in your functions.php theme file.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂
if ( current_user_can('contributor') && !current_user_can('upload_files') )
	add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads() {
	$contributor = get_role('contributor');
	$contributor->add_cap('upload_files');
}

Include Featured image in your RSS feed.

If you want to include an image in your RSS feed for feedburner you can include the following code in your functions.php file.

//Function to add featured image in RSS feeds
function featured_image_in_rss($content)
{
    // Global $post variable
    global $post;
    // Check if the post has a featured image
    if (has_post_thumbnail($post-ID))
    {
        $content = get_the_post_thumbnail($post-ID, 'full', array('style' ='margin-bottom:10px;')) . $content;
    }
    return $content;
}
//Add the filter for RSS feeds Excerpt
add_filter('the_excerpt_rss', 'featured_image_in_rss');
//Add the filter for RSS feed content
add_filter('the_content_feed', 'featured_image_in_rss');

OR

Subscribe To My YouTube Channel:
	function add_featured_image_to_feed($content) {
		global $post;
		if ( has_post_thumbnail( $post->ID ) ){
			$content = '' . get_the_post_thumbnail( $post->ID, 'large' ) . '' . $content;
		}
		return $content;
	}

	add_filter('the_excerpt_rss', 'add_featured_image_to_feed', 1000, 1);
	add_filter('the_content_feed', 'add_featured_image_to_feed', 1000, 1);

Micro Data “Missing: updated” in Google Webmaster Tools

Well if you ever saw this pain in the keester you would understand many frustration. Below are the steps to fix it.

missing updated google webmaster tools wordpress FIX

  1. Go to your dashboard and scroll down to appearance.
  2. Hover over it and click on editor.
  3. On the right look for single.php
  4. MAKE A BACK UP OF THE FILE! You can copy the entire code of that page and put it in notepad or something.
  5. Add this line of code within the loop.
<span class="date updated"><?php echo get_the_date(); ?></span>

Update Jquery in WordPress

if( !is_admin()){
wp_deregister_script('jquery');
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false, '1.9.1');
wp_enqueue_script('jquery');

or

if ( !is_admin() ) // using a newer version of jQuery will break stuff in WP admin
     {
      wp_deregister_script('jquery');
      wp_enqueue_script('jQuery', WP_PLUGIN_URL . '/wp-store-locator/js/jQuery.js');

     }

Function in WordPress to fix micro data in Google Webmaster Tools

There is an update error in most WordPress themes. Add the following code to yoru child theme to fix this error.

//mod content
function hatom_mod_post_content ($content) {
  if ( in_the_loop() && !is_page() ) {
    $content = '<span class="entry-content">'.$content.'</span>';
  }
  return $content;
}
add_filter( 'the_content', 'hatom_mod_post_content');

//add hatom data
function add_mod_hatom_data($content) {
    $t = get_the_modified_time('F jS, Y');
    $author = get_the_author();
    $title = get_the_title();
    if(is_single()) {
        $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
    }
    return $content;
    }
add_filter('the_content', 'add_mod_hatom_data');

Get Rid of howdy in WordPress

function remove_howdy( $wp_admin_bar ) {
    $my_account=$wp_admin_bar->get_node('my-account');
    $newtitle = str_replace( 'Howdy,', '', $my_account->title );
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title' => $newtitle,
    ) );
}
add_filter( 'admin_bar_menu', 'remove_howdy',25 );

Increase the Memory size in WordPress with the config file

Add this code at the bottom of your config file. This will increase your memory limit.

/** Memory Limit */
define('WP_MEMORY_LIMIT', '96M');

Arrange WordPRess Posts by Date

// Runs before the posts are fetched
add_filter( 'pre_get_posts' , 'my_change_order' );
// Function accepting current query
function my_change_order( $query ) {
	// Check if the query is for an archive
	if($query->is_archive)
		// Query was for archive, then set order
		$query->set( 'order' , 'asc' );
	// Return the query (else there's no more query, oops!)
	return $query;
}

Create a post Template for WordPress

<?php
/*
Single Post Template: [Descriptive Template Name]
Description: This part is optional, but helpful for describing the Post Template
*/
?>

Page Break in Visual Editor Quick Code For Pagniation

 

// add <!-- next-page --> button to tinymce
add_filter('mce_buttons','wysiwyg_editor');
function wysiwyg_editor($mce_buttons) {
    $pos = array_search('wp_more',$mce_buttons,true);
    if ($pos !== false) {
        $tmp_buttons = array_slice($mce_buttons, 0, $pos+1);
        $tmp_buttons[] = 'wp_page';
        $mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1));
    }
    return $mce_buttons;
}

PHP code to make a feed on a sub domain

<?php
$opts = array(
 'http'=>array(
 'method'=>"GET",
 'header'=> "User-Agent: FeedBurner/1.0\r\n"
 )
);

$context = stream_context_create($opts);

$feed = file_get_contents("https://a1websitepro.com/feed",false,$context);

echo $feed;
?>

Here is a little trick that you can use to disable pretty photo on certain images

Basically you have to use a class=”noLighbox”

<a title="A1WEBSITEPRO" href="https://a1websitepro.com" class="noLightbox">
<img title="WEB DEVELOPMENT" alt="" src="https://a1websitepro.com/wp-content/uploads/2014/09/logo-web-design-and-develoopment.png" />
</a>

 

Remove links on all pictures

add_filter( 'the_content', 'attachment_image_link_remove_filter' );

function attachment_image_link_remove_filter( $content ) {
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\ uploads)[^="">]*><img}',
'{ wp-image-[0-9]*" />}'),
array('<img','" />'),
$content
);
return $content;
}

Speed Up Dashboard by Removing Meta Information

function remove_dashboard_meta() {
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
}
add_action( 'admin_init', 'remove_dashboard_meta' );

 Increase Max File Size with php.ini file

upload_max_filesize = 32M

post_max_size = 32M 

Disable Heartbeat in Dashboard

add_action( 'init', 'stop_heartbeat', 1 );

function stop_heartbeat() {
        wp_deregister_script('heartbeat');
}

Add Content Before or After Each Post

Before Content

//add content before post

function insert_the_content( $content ) {
 $custom_content = 'YOUR CONTENT GOES HERE';
 $custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'insert_the_content' ); 

After Content

//add content after post
function insert_the_content( $content ) {
 $custom_content = $content;
 $custom_content .= 'YOUR CONTENT GOES HERE';
 return $custom_content;
}
add_filter( 'the_content', 'insert_the_content' );

Add Content to Title

// add content to title
function theme_slug_filter_the_title( $title ) {
$custom_title = 'YOUR CONTENT GOES HERE';
$title .= $custom_title;
return $title;
}
add_filter( 'the_title', 'theme_slug_filter_the_title' );

Create a New user in WordPress with PHP

Create user File Download

Add No Follow links in wordpress

add_filter( 'the_content', 'cn_nf_url_parse');

function cn_nf_url_parse( $content ) {

 $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
 if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
 if( !empty($matches) ) {

 $srcUrl = get_option('home');
 for ($i=0; $i < count($matches); $i++)
 {

 $tag = $matches[$i][0];
 $tag2 = $matches[$i][0];
 $url = $matches[$i][0];

 $noFollow = '';

 $pattern = '/target\s*=\s*"\s*_blank\s*"/';
 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
 if( count($match) < 1 )
 $noFollow .= ' target="_blank" ';

 $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
 if( count($match) < 1 )
 $noFollow .= ' rel="nofollow" ';

 $pos = strpos($url,$srcUrl);
 if ($pos === false) {
 $tag = rtrim ($tag,'>');
 $tag .= $noFollow.'>';
 $content = str_replace($tag2,$tag,$content);
 }
 }
 }
 }

 $content = str_replace(']]>', ']]>', $content);
 return $content;

}

Remove Author URL from comment forms

add_filter( 'get_comment_author_url', '__return_null' );

Duplicate Meta Descriptions and Titles Function to add a page number so you dont get in trouble for duplicate descriptions when using pagination.

if ( ! function_exists( 'maximus_page_number' ) )
{
function maximus_page_number( $s )
{
global $page;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
! empty ( $page ) && 1 < $page && $paged = $page;

$paged > 1 && $s .= ' | ' . sprintf( __( 'Page: %s' ), $paged );

return $s;
}

add_filter( 'wp_title', 'maximus_page_number', 100, 1 );
add_filter( 'wpseo_metadesc', 'maximus_page_number', 100, 1 );
}

Insert Ads After Paragraphs in WordPress Posts

You can see when you go to my posts that I have ads after the second paragraphs. how did I do this? With the following code! 🙂 Thanks to WP-Beginner for this code! 🙂

//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}

	return $content;
}

// Parent Function that makes the magic happen

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
	$closing_p = '</p>';
	$paragraphs = explode( $closing_p, $content );
	foreach ($paragraphs as $index => $paragraph) {

		if ( trim( $paragraph ) ) {
			$paragraphs[$index] .= $closing_p;
		}

		if ( $paragraph_id == $index + 1 ) {
			$paragraphs[$index] .= $insertion;
		}
	}

	return implode( '', $paragraphs );
}

Remove All Featured Images From WordPress

global $wpdb;
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
" );

Set First Image in Post as Featured Image

function wpsites_auto_set_featured_image() {
 global $post;
 $featured_image_exists = has_post_thumbnail($post->ID);
 if (!$featured_image_exists) {
 $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
 if ($attached_image) {
 foreach ($attached_image as $attachment_id => $attachment) {
 set_post_thumbnail($post->ID, $attachment_id);
 }
 }
 }
 }
add_action('the_post', 'wpsites_auto_set_featured_image');

Read More Link for Genesis

// Add Read More Link to Excerpts
add_filter('excerpt_more', 'get_read_more_link');
add_filter( 'the_content_more_link', 'get_read_more_link' );
function get_read_more_link() {
 return '... <a href="' . get_permalink() . '">[Read More]</a>';
}

Function for Favicon in WordPress

// add a favicon to your site
function blog_favicon() {
	echo '<a href="link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/images/favicon.ico" />' . "\n";
}
add_action('wp_head', 'blog_favicon');

Make shortcodes work in Sidebar Widgets.

add_filter('widget_text', 'do_shortcode');

Disable Emojis WordPress Junk

function disable_wp_emojicons() {

  // all actions related to emojis
  remove_action( 'admin_print_styles', 'print_emoji_styles' );
  remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  remove_action( 'wp_print_styles', 'print_emoji_styles' );
  remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );

  // filter to remove TinyMCE emojis
  add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
add_action( 'init', 'disable_wp_emojicons' );

function disable_emojicons_tinymce( $plugins ) {
  if ( is_array( $plugins ) ) {
    return array_diff( $plugins, array( 'wpemoji' ) );
  } else {
    return array();
  }
}

Remove WordPress Customizer

class Quest_Customize {

		// Hold an instance of the class
		private static $instance;

			/**
		 * Returns an instance of the Quest_Customize class, creates one if an instance doesn't exist. Implements Singleton pattern
		 *
		 * @return Quest_Customize
		 */
		public static function getInstance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new static ();
			}

			return self::$instance;
		}

}

Remove custom backgrounds and header image in child themes

add_action( 'init', 'remove_crap' );
    function remove_crap() {

    remove_custom_image_header();
    remove_custom_background();
   // remove_theme_support('post-formats');
}

Remove Stylesheets from WordPress Plugins

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
	wp_deregister_style( 'wp-pagenavi' );
}

Remove JavaScript from WordPress Plugins

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
	wp_deregister_script( 'contact-form-7' );
}

Add Custom Background In Functions.php file

add_theme_support( 'custom-background' );

Remove clickable URL’s in the comments section of your website

function remove_links($string = '') {
	$link_pattern = "/<a[^>]*>(.*)<\/a>/iU";
	$string = preg_replace($link_pattern, "$1", $string);

	return $string;
}

add_filter('comment_text', 'remove_links');

Adding Content To Home Page After 1st and 3rd Paragraphs

Genesis Only

/*adding widgets on home page*/

add_action( 'genesis_after_entry', 'addtl_widget_area' );
function addtl_widget_area() {
        global $loop_counter;
        $loop_counter++;
        if( (!is_paged() && !is_singular()) && ($loop_counter == 1)) {
                echo '<div style="margin-bottom:25px;"><p>Test 1</p></div>';
                $loop_counter = $loop_counter + 1;
        }
        if( (!is_paged() && !is_singular()) && ($loop_counter == 4)) {
                echo '<div style="margin-bottom:25px;"><p>Test 2</p></div>';
                $loop_counter = $loop_counter + 1;
        } 
}

Show All Categories On A PHP Page

$categories = get_categories();
foreach($categories as $category) {
   echo '<div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>';
}

More WordPress functions to come!

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share: