Tips + Tricks + Ads – Functions.php / Enque / Header / Footer


/* ////////////////////////////////////////////*/
/* /////////////  SIMPLE TRICKS / TIPS  ////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////*/







 /*------------------------------------*
CUSTOMIZE LOGIN PAGE
 *------------------------------------*/

/*

a) In your current theme directory (../wp-content/themes/your-theme-name), add a folder called “login”. Create a CSS file inside the login folder and name it custom-login-styles.css
b) Next, add the following code into your functions.php file




*/

function my_custom_login() {
echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('stylesheet_directory') . '/login/custom-login-styles.css" />';
}
add_action('login_head', 'my_custom_login');




 /*------------------------------------*
Hide Login Errors in WordPress
 *------------------------------------*/

/*


Login errors in WordPress can be used by hackers to guess whether they entered wrong username or password. By hiding login errors in WordPress you can make your login area a bit more secure.

Now users see a generic message when they enter incorrect username or password.

*/

/* version 1 */

function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );



/* version 2 */

function no_wordpress_errors(){
  return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );



 /*------------------------------------*
Disable Login by Email in WordPress
 *------------------------------------*/

/*


WordPress allows users to login with username or email address. You can easily disable login by email in WordPress by adding this code to your functions file.

helps with securiyt 

*/

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );


 /*------------------------------------*
DISABLE SEARCH FEATURE
 *------------------------------------*/

/*

Disable Search Feature in WordPress
If you want to disable search feature on your WordPress site, then simply add this code to your functions file.

*/

/* version 1 */

function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );


/* version 2 */
function fb_filter_query( $query, $error = true ) {
 
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
 
// to error
if ( $error == true )
$query->is_404 = true;
}
}
 
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );






 /*------------------------------------*
 ADD TO USER DATA - more fields for users
 *------------------------------------*/

/*
Add Author Profile Fields
Do you want to add extra fields to your author profiles in WordPress? You can easily do that by adding this code to your functions file:

This code will add Twitter and Facebook fields to user profiles in WordPress.

https://www.wpbeginner.com/plugins/how-to-add-additional-user-profile-fields-in-wordpress-registration/

*/

function wpb_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';

return $contactmethods;
}
add_filter('user_contactmethods','wpb_new_contactmethods',10,1);

/*You can now display these fields in your author template like this:*/

/* <?php echo $curauth->twitter; ?> */

/*
Add social profile information on the user profile page
By default, the user profile page in the dashboard has fields to add the contact info including AIM, Yahoo IM, Jabber/Google Talk, etc. Open the functions file and add the following snippet to add more social media fields in the user profile page.
*/

function my_new_contactmethods( $contactmethods ) {

// Add Twitter

$contactmethods['twitter'] = 'Twitter';

//add Facebook

$contactmethods['facebook'] = 'Facebook';

return $contactmethods;

}

add_filter('user_contactmethods','my_new_contactmethods',10,1);

You can use the following code in author.php file to display it.

echo $curauth->twitter;



 /*------------------------------------*
ADD  IMAGE SIZES - cant add to file - throws errors - see link - 
 *------------------------------------*/

/*

Add Additional Image Sizes in WordPress
WordPress automatically creates several image sizes when you upload an image. You can also create additional image sizes to use in your theme. Add this code your theme’s functions file.

Creates three new image sizes with different sizes. Feel free to tweak the code to meet your own requirements.

You can display an image size in anywhere in your theme using this code.
https://www.wpbeginner.com/wp-tutorials/how-to-create-additional-image-sizes-in-wordpress/

*/




 /*------------------------------------*
RANDOM IMAGE IN HEADER
 *------------------------------------*/

/*

 Display random image header
If you are a person who would love to display random image headers on your blog, this trick is for you.
Name your image design-assets/ago-no-category/1.jpg, 2.jpg, 3.jpg, and so on. Upload those images to images folder inside your theme directory. 

Make sure you replace the Path_to_image_folder with the actual path.

*/

/*
Then, paste the following code to the header file.

<img src="http://Path_to_image_folder/<?php echo(rand(1,10)); ?>.jpg" width="image_width" height="image_height" alt="image_alt_text" />

*/




 /*------------------------------------*
 	REMOVE VERSION #
 *------------------------------------*/


/*
Remove WordPress Version Number
https://www.wpbeginner.com/wp-tutorials/the-right-way-to-remove-wordpress-version-number/
*/

function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');



 /*------------------------------------*
 ADDING CUSTOM DASHBOARD LOGO
 *------------------------------------*/

/*
adding custom dash board logo
https://www.wpbeginner.com/wp-themes/adding-a-custom-dashboard-logo-in-wordpress-for-branding/

*/

function wpb_custom_logo() {
echo '
<style type="text/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-clickety-clack-site/june2021/logo.png) !important;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
background-position: 0 0;
}
</style>
';
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpb_custom_logo');

 /*------------------------------------*
 REMOVE HLEP SCREEN FROM DASHBOARD - CLEARN UP CLUTTER
 *------------------------------------*/

/*


Remove help and screen options from dashboard
This WordPress tip cleans your admin dashboard from unnecessary clutter. You can see the options “help” and “screen” on the top right hand side of your WP dashboard. Add the following code to functions to remove these options from the dashboard.
*/

add_filter( 'contextual_help', 'wpse_25034_remove_dashboard_help_tab', 999, 3 );

add_filter( 'screen_options_show_screen', 'wpse_25034_remove_help_tab' );

function wpse_25034_remove_dashboard_help_tab( $old_help, $screen_id, $screen )

  {

     if( 'dashboard' != $screen->base )

     return $old_help;

     $screen->remove_help_tabs();

     return $old_help;

  }

function wpse_25034_remove_help_tab( $visible )

  {

     global $current_screen;

     if( 'dashboard' == $current_screen->base )

     return false;

     return $visible;

  }





 /*------------------------------------*
CHANGE WP FOOTER IN ADMIN
 *------------------------------------*/

/*
can make the footer special for you - remove wp -

*/

/* version 1 */
function remove_footer_admin () {
echo 'Hello world!';
}
add_filter('admin_footer_text', 'remove_footer_admin');


/* version 2 */
function remove_footer_admin () {

echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Tutorials: <a href="https://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';

}

add_filter('admin_footer_text', 'remove_footer_admin');




 /*------------------------------------*
 ADD CUSTOM WIDGETS TO DASHBOARD (ADMIN)
 *------------------------------------*/

/*

You probably have seen widgets that numerous plugins and themes add in the WordPress dashboard. As a theme developer, you can add one yourself by pasting the following code:

*/

/* version 1 */

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_widget', 'More Information', 'custom_dashboard_information');
}
function custom_dashboard_information() {
echo 'If you need help making changes to your site, you can always contact me via email at...';
}


/* version 2 */
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

function my_custom_dashboard_widgets() {
global $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help');
}

function custom_dashboard_help() {
echo '<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="https://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}



 /*------------------------------------*
 FIX GRAVITARS - USE A BRANDED THING
 *------------------------------------*/

/*
Have you seen the default mystery man avatar on blogs? You can easily replace it with your own branded custom avatars. Simply upload the image you want to use as default avatar and then add this code to your functions file.


*/

add_filter( 'avatar_defaults', 'wpb_new_gravatar' );
function wpb_new_gravatar ($avatar_defaults) {
$myavatar = 'http://example.com/wp-content/uploads/2017/01/wpb-default-gravatar.png';
$avatar_defaults[$myavatar] = "Default Gravatar";
return $avatar_defaults;
}

 /*------------------------------------*
 DYNAMIC COPYWRITE
 *------------------------------------*/

/*
After adding this function, you’ll need to open your footer.php file and add the following code wherever you like to display the dynamic copyright date:

<?php echo wpb_copyright(); ?>
This function looks for the date of your first post, and the date of your last post. It then echos the years wherever you call the function.


*/

function wpb_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}



 /*------------------------------------*
 RANDOMIZE BACKGROUDN COLOR
 *------------------------------------*/

/*
Randomly Change Background Color in WordPress
Do you want to randomly change background color on your WordPress upon each visit and page reload? Here is how to easily do this.

First you need to add this code to your theme’s functions file.

Next, you’ll need to edit the header.php file in your theme. Locate the <body> tag and add replace it with this line:

<body <?php body_class(); ?> style="background-color:<?php wpb_bg();?>">>


*/



function wpb_bg() {
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color ='#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].
$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo $color;
}

 /*------------------------------------*
 UPDATE SITE URLS (if you get locked out
 *------------------------------------*/

/*

Update WordPress URLs
If your WordPress login page keeps refreshing or you are unable to access admin area, then you need to update WordPress URLs.

One way to do this is by using wp-config.php file. However, if you do that you will not be able to set the correct address on the settings page. The WordPress URL and Site URL fields will be locked and uneditable.

If you want to fix this, then you should add this code to your functions file.

Once you are logged in, you can go to Settings and set the URLs there. After that you should remove the code you added to the functions file, otherwise it will keep updating those URLs any time your site is accessed.

*/


update_option( 'siteurl', 'http://example.com' );
update_option( 'home', 'http://example.com' );

/*Don’t forget to replace example.com with your own domain name*/


 /*------------------------------------*
 ALL AUTHORS PAGE
 *------------------------------------*/

/*


List all authors of your blog in a page
Add the following code where you want to display the list of all authors of your blog.
*/

<ul>

<?php wp_list_authors('exclude_admin=0&optioncount=1&show_fullname=1&hide_empty=1'); ?>

</ul>



 /*------------------------------------*
 GUEST AUTHORS
 *------------------------------------*/

/*

Display guest author’s name via custom fields
Most guest authors are onetime publishers, especially if you are taking guest posts on your site. So there is no point in creating a separate profile for them. Rather, add the following code to single.php where you’d like to display the author name. You can display all guest posts using this description.

*/
<?php $author = get_post_meta($post->ID, "guest-author", true);

if ($author != "") {

echo $author;

} else {

the_author();

} ?>

/*
Once done, create a custom field named guest-author and type your custom title as a value.
*/





 /*------------------------------------*
CHANGE READ MORE TEXT TO EXCERPTS
 *------------------------------------*/

/*

Change Read More Text for Excerpts in WordPress
Do you want to change the text that appears after the excerpt? Simply add this code to your theme’s functions file.

*/

function modify_read_more_link() {
    return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );








 /*------------------------------------*
CHANGE EXCERPT LENGTH
 *------------------------------------*/

/*
Change Excerpt Length in WordPress
WordPress limits excerpt lengths to 55 words. If you need to change that, then you can add this code to your functions file.


For alternate method, you may want to take a look at our guide on how to customize WordPress excerpts (no coding required).

https://www.wpbeginner.com/plugins/how-to-customize-wordpress-excerpts-no-coding-required/
*/

/* version 1 */

functionnew_excerpt_length($length) {
return 75;
}
add_filter('excerpt_length', 'new_excerpt_length');



/* version 2 */
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
Change 100 to the number of words you want to show in the excerpts.




 /*------------------------------------*
ADD ADMIN USER WITH FTP  / FUCNTIONS PHP
 *------------------------------------*/

/*
 Add an Admin User in WordPress
If you have forgotten your WordPress password and email, then you can add an admin user by adding this code to your theme’s functions file using an FTP client.


For more on this topic, take a look at our tutorial on how to add an admin user in WordPress using FTP.


*/

function wpb_admin_account(){
$user = 'username';
$pass = 'password';
$email = 'email@mywordpresssite.com';
if ( !username_exists( $user )  && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','wpb_admin_account');

/*
Don’t forget to fill in the username, password, and email fields. Once you login to your WordPress site, don’t forget to delete the code from your functions file.
*/


   /*------------------------------------*
   RESET PASSWORD MANAUALLY
   *------------------------------------*/
/*

/*
Log in to phpMyAdmin, select the website database, click SQL and and paste the following command.

UPDATE `wp_users` SET `user_pass` = MD5('PASSWORD') WHERE `wp_users`.`user_login` =`admin` LIMIT 1;

Note: You need to change the ‘admin’ to your actual username and PASSWORD to your preferred password.

*/


 /*------------------------------------*
REMOVE WELCOME PANEL FROM DASHBOARD
 *------------------------------------*/

/*


*/


remove_action('welcome_panel', 'wp_welcome_panel');





 /*------------------------------------*
TOTAL NUMBER OF REG USERS
 *------------------------------------*/

/*

Show Total Number of Registered Users in WordPress
Do you want to show total number of registered users on your WordPress site? Simply add this code to your theme’s functions file.

For more information and a plugin method, see our tutorial on how to display total number of registered users in WordPress.

*/

// Function to return user count
function wpb_user_count() {
$usercount = count_users();
$result = $usercount['total_users'];
return $result;
}
add_shortcode('user_count', 'wpb_user_count');

/*
Creating a shortcode to display user count
add_shortcode('user_count', 'wpb_user_count');
This code creates a shortcode that allows you to display total number of registered users on your site. Now you just need to add this shortcode to [user_count] your post or page where you want to show the total number of users.
*/



 /*------------------------------------*
ENABLE SHORTCODE EXECUTION IN TEXT WIDGETS
 *------------------------------------*/

/*

Enable Shortcode Execution in Text Widgets
By default, WordPress does not execute shortcodes inside text widgets. To fix this you need to simply add this code to your theme’s functions file.

For an alternate method and more information, take a look at our guide on how to use shortcodes in WordPress sidebar widgets.
https://www.wpbeginner.com/wp-tutorials/how-to-use-shortcodes-in-your-wordpress-sidebar-widgets/

*/

// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');



 /*------------------------------------*
ODD EVEN CLASSES TO STYLE POSTS
 *------------------------------------*/

/*

Add Odd and Even CSS Classes to WordPress Posts
You may have seen WordPress themes using an old or even class for WordPress comments. It helps users visualize where one comment ends and the next one begins.

You can use the same technique for your WordPress posts. It looks aesthetically pleasing and helps users quickly scan pages with lots of content. Simply add this code to your theme’s functions file.

https://www.wpbeginner.com/wp-themes/how-to-add-oddeven-class-to-your-post-in-wordpress-themes/

*/


function oddeven_post_class ( $classes ) {
   global $current_class;
   $classes[] = $current_class;
   $current_class = ($current_class == 'odd') ? 'even' : 'odd';
   return $classes;
}
add_filter ( 'post_class' , 'oddeven_post_class' );
global $current_class;
$current_class = 'odd';

/*
This code simply adds an odd or even class to WordPress posts. You can now add custom CSS to style them differently. Here is a sample code to help you get started.


.even {
background:#f0f8ff;  
} 
.odd {
 background:#f4f4fb;
}

*/




 /*------------------------------------*
REMOVE IMAGE LINKS FOR ALL IMAGES
 *------------------------------------*/

/*

Remove Default Image Links in WordPress
By default, when you upload an image in WordPress it is automatically linked to the image file or the attachment page. If users click on the image they are then taken to a new page away from your post.

Here is how you can easily stop WordPress from automatically linking image uploads. All you have to do is to add this code snippet to your functions file:

*/

/* version 1 */

function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);


/* version 2 */

function wpb_imagelink_setup() {
    $image_set = get_option( 'image_default_link_type' );
     
    if ($image_set !== 'none') {
        update_option('image_default_link_type', 'none');
    }
}
add_action('admin_init', 'wpb_imagelink_setup', 10);


/* version 3 */

function wpb_imagelink_setup() {
    $image_set = get_option( 'image_default_link_type' );
     
    if ($image_set !== 'none') {
        update_option('image_default_link_type', 'none');
    }
}
add_action('admin_init', 'wpb_imagelink_setup', 10);





 /*------------------------------------*
AUTHOR BOX - SO THEY CAN STAND OUT MORE
 *------------------------------------*/

/*

Add an Author Info Box in WordPress Posts
If you run a multi-author site and want to showcase author bios at the end of your post, then you can try this method. Start by adding this code to your functions file:

*/




function wpb_author_info_box( $content ) {
 
global $post;
 
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
 
// Get author's display name 
$display_name = get_the_author_meta( 'display_name', $post->post_author );
 
// If display name is not available then use nickname as display name
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );
 
// Get author's biographical information or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );
 
// Get author's website URL 
$user_website = get_the_author_meta('url', $post->post_author);
 
// Get link to the author archive page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
  
if ( ! empty( $display_name ) )
 
$author_details = '<p class="author_name">About ' . $display_name . '</p>';
 
if ( ! empty( $user_description ) )
// Author avatar and bio
 
$author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>';
 
$author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';  
 
// Check if author has a website in their profile
if ( ! empty( $user_website ) ) {
 
// Display author website link
$author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
 
} else { 
// if there is no author website then just close the paragraph
$author_details .= '</p>';
}
 
// Pass all this info to post content  
$content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>';
}
return $content;
}
 
// Add our function to the post content filter 
add_action( 'the_content', 'wpb_author_info_box' );
 
// Allow HTML in author bio section 
remove_filter('pre_user_description', 'wp_filter_kses');

/*
Next you will need to add some custom CSS to make it look better. You can use this sample CSS as an starting point.

.author_bio_section{
background: none repeat scroll 0 0 #F5F5F5;
padding: 15px;
border: 1px solid #ccc;
}
 
.author_name{
font-size:16px;
font-weight: bold;
}
 
.author_details img {
border: 1px solid #D8D8D8;
border-radius: 50%;
float: left;
margin: 0 10px 10px 0;
}

*/




 /*------------------------------------*
DISABLE XML-RPC
 *------------------------------------*/

/*

. Disable XML-RPC in WordPress
XML-RPC is a method that allows third party apps to communicate with your WordPress site remotely. This could cause security issues and can be exploited by hackers.

Simply add this code to your functions file to turn off XML-RPC in WordPress:
You may want to read our article on how to disable XML-RPC in WordPress for more information.

*/

add_filter('xmlrpc_enabled', '__return_false');







 /*------------------------------------*
LINK FEAT IMAGES TO POSTS - IF THEY ARE NOT
 *------------------------------------*/

/*

Automatically Link Featured Images to Posts
If your WordPress theme does not automatically link featured images to full articles, then you can try this method. Simply add this code to your theme’s functions file.

*/


function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
 
If (! is_singular()) { 
     
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
 
} else { 
 
return $html;
 
}
 
}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );






 /*------------------------------------*
CHANGE CATEGORY CHECKBOX TO RADIO
 *------------------------------------*/

/*



*/


add_action('add_meta_boxes','bptproject_add_meta_boxes',10,2);
function bptproject_add_meta_boxes($post_type, $post) {
    if ('bptproject'==$post_type) {
        ob_start();
    }
}
add_action('dbx_post_sidebar','bptproject_dbx_post_sidebar');
function bptproject_dbx_post_sidebar() {
    $html = ob_get_clean();
    $html = str_replace('"checkbox"','"radio"',$html);
    echo $html;
}









   /*------------------------------------*
   HIDE ADS FOR SINGLE POST
   *------------------------------------*/

/* 

/*
If you are displaying advertisement on every blog post and want to hide ads for a certain post, just add the following code snippet to your single.php file. Make sure you replace xx with the post id and insert your ad code to below code snippet.

if(get_the_ID() != xx) {
Your ad code here
}

*/


   /*------------------------------------*
   embed ad inside excerpts of first blog post
   *------------------------------------*/

/* 

56. Embed ad inside the excerpts of first blog post

In the homepage, you can place an advertisement inside the excerpts of your first blog post. This is a bit aggressive so handle with care, make sure you only have one advert above the fold, because this WordPress tip can land you in hot water with Google and can result in a penalty if it comes at the expense of the user experience.


Open index.php file and find <?php if (have_posts()) 

Add the following line above it:

<?php $count = 1; ?>

Then, find the code that starts with

<?php the_content
Add the following code after the closing tag, ?>

<?php if ($count == 1) : ?>

AD CODE

<?php endif; $count++; ?>

Make sure you replace AD CODE with your advertisement code.

/*


   /*------------------------------------*
  WRAP ADS IN POSTS
   *------------------------------------*/

/* 

In functions.php file, add the below code snippet. In addition, make sure you insert your ad codes inside it.

*/

function googleadsense($content){

  $adsensecode = 'Your Ad Codes Here';

  $pattern = '<!-googlead->';

  $content = str_replace($pattern, $adsensecode, $content);

  return $content;      

}

add_filter('the_content', 'googleadsense');

/*

Insert <!-googlead-> in your posts and pages wherever you want to display the ad.
*/



   /*------------------------------------*
   ADD AD CONTENT ABOVE ARTICLES
   *------------------------------------*/
/*
Add promotional content in homepage above the articles
In index.php file, find the following code: <div class="content-loop">. Add your promotional content above it, whether it is email newsletter form, advertisement, etc.
*/





   /*------------------------------------*
   DISPLAY ADS USING SHORTCODES
   *------------------------------------*/

/* 
Insert the shortcode, [adsense] anywhere inside the posts and pages content where you want the ads to be displayed. Just add the below-given lines to your functions.php file.

function showads() {

    return '

ADS CODE HERE

';

}

add_shortcode('adsense', 'showads');

*/




   /*------------------------------------*
   AD SENSE ADS
   *------------------------------------*/

/* 

54. Display AdSense ads only to search engines
If you look at the total ad clicks you receive on your website, you'll find that search engines visitors are more likely to click the contextual ads like AdSense than any other visitor. So restricting your ad to display only to the visitors that come from search engine can help you increase the CTR of your ad.
To display ads only to search engine visitors, add the following code to your functions.php file.

$ref = $_SERVER['HTTP_REFERER'];

$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');

foreach ($SE as $source) {

  if (strpos($ref,$source)!==false) {

    setcookie("sevisitor", 1, time()+3600, "/", ".yourdomain.com"); 

    $sevisitor=true;

  }

}

function visits_from_searchengine(){

  global $sevisitor;

  if ($sevisitor==true || $_COOKIE["sevisitor"]==1) {

    return true;

  }

  return false;

}

Make sure you change yourdomain.com to your domain. Then, add the following code snippet to single.php file.

<?php if (function_exists('visits_from_searchengine')) {

  if (visits_from_searchengine()) { ?>

YOUR CODE HERE

<?php } } ?>

Don’t forget to add your AdSense code by replacing YOUR CODE HERE.

*/























 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/




















Scroll to Top