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.

[code]
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’);
}[/code]

Only cool people share!

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.

[code]//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’);
[/code]

OR

[code] 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);[/code]

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.

[code]<span class="date updated"><?php echo get_the_date(); ?></span>[/code]

Update Jquery in WordPress

[code]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’);[/code]

or

[code]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’);

}[/code]

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.

[code]//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’);[/code]

Get Rid of howdy in WordPress

[code]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 );[/code]

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.

[code]/** Memory Limit */
define(‘WP_MEMORY_LIMIT’, ’96M’);[/code]

Arrange WordPRess Posts by Date

[code]// 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;
}[/code]

Create a post Template for WordPress

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

Page Break in Visual Editor Quick Code For Pagniation

 

[code]// 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;
}[/code]

PHP code to make a feed on a sub domain

[code]<?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;
?>[/code]

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

Basically you have to use a class=”noLighbox”

[code]<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>[/code]

 

Remove links on all pictures

[code]

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;
}[/code]

Speed Up Dashboard by Removing Meta Information

[code]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’ );[/code]

 Increase Max File Size with php.ini file

[code]

upload_max_filesize = 32M

post_max_size = 32M [/code]

Disable Heartbeat in Dashboard

[code]add_action( ‘init’, ‘stop_heartbeat’, 1 );

function stop_heartbeat() {
wp_deregister_script(‘heartbeat’);
}[/code]

Add Content Before or After Each Post

Before Content

[code]//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’ );

[/code]

After Content

[code]//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’ );[/code]

Add Content to Title

[code]// 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’ );
[/code]

Create a New user in WordPress with PHP

Create user File Download

Add No Follow links in wordpress

[code]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;

}[/code]

Remove Author URL from comment forms

[code]add_filter( ‘get_comment_author_url’, ‘__return_null’ );[/code]

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

[code]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 );
}[/code]

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! 🙂

[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 );
}[/code]

Remove All Featured Images From WordPress

[code]global $wpdb;
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = ‘_thumbnail_id’
" );[/code]

Set First Image in Post as Featured Image

[php]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’);[/php]


Read More Link for Genesis

[php]// 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>’;
}[/php]

Function for Favicon in WordPress

[php]// 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’);[/php]

Make shortcodes work in Sidebar Widgets.

[php]add_filter(‘widget_text’, ‘do_shortcode’);[/php]

Disable Emojis WordPress Junk

[php]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();
}
}[/php]

Remove WordPress Customizer

[php]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;
}

}[/php]

Remove custom backgrounds and header image in child themes

[php]add_action( ‘init’, ‘remove_crap’ );
function remove_crap() {

remove_custom_image_header();
remove_custom_background();
// remove_theme_support(‘post-formats’);
}[/php]

Remove Stylesheets from WordPress Plugins

[php]add_action( ‘wp_print_styles’, ‘my_deregister_styles’, 100 );
function my_deregister_styles() {
wp_deregister_style( ‘wp-pagenavi’ );
}[/php]

Remove JavaScript from WordPress Plugins

[code]add_action( ‘wp_print_scripts’, ‘my_deregister_javascript’, 100 );
function my_deregister_javascript() {
wp_deregister_script( ‘contact-form-7’ );
}[/code]

Add Custom Background In Functions.php file

[code]add_theme_support( ‘custom-background’ );[/code]

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

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

return $string;
}

add_filter(‘comment_text’, ‘remove_links’);[/code]

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

Genesis Only

[code]/*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;
}
}[/code]

Show All Categories On A PHP Page

[php]$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>’;
}[/php]

More WordPress functions to come!