Pages + Templates – Functions.php / Enque / Header / Footer

/* ////////////////////////////////////////////*/
/* /////////////  PAGES TEMPLATES ETC   ////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////*/


 /*------------------------------------*
ADD WIDGET AREA - LONG VERSION
 *------------------------------------*/

/*
https://www.collectiveray.com/wp/tips/how-to-add-a-widget-area-to-the-twenty-fifteen-theme-footer
shows all parts


*/

/**
 * Register footer widget area.
 *
 * @since Twenty Fifteen Child 1.0
 *
 * @link https://codex.wordpress.org/Function_Reference/register_sidebar
 */
function twentyfifteen_child_widgets_init() {
  register_sidebar( array(
    'name'          => __( 'Footer Widgets', 'twenty-fifteen-child' ),
    'id'            => 'sidebar-2',
    'description'   => __( 'Add widgets here to appear in your footer area.', 'twenty-fifteen-child' ),
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget'  => '</aside>',
    'before_title'  => '<h2 class="widget-title">',
    'after_title'   => '</h2>',
  ) );
}
add_action( 'widgets_init', 'twentyfifteen_child_widgets_init' );


/*
Open footer.php in a code editor and enter this snippet just below this line of code: <footer id="colophon" class="site-footer" role="contentinfo">
*/

/*
<?php if ( is_active_sidebar( 'sidebar-2' )  ) : ?>

 <div class="widget-area" role="complementary">
 
  <?php dynamic_sidebar( 'sidebar-2' ); ?>
 
 </div>
 
<?php endif; ?>
*/




 /*------------------------------------*
 ADDING WIDGET AREAS / SIDEBARS
 *------------------------------------*/

/*

https://www.wpbeginner.com/wp-themes/how-to-add-dynamic-widget-ready-sidebars-in-wordpress/

*/

/*////////////// VERISON 1 */

function custom_sidebars() {
    $args = array(
        'id'            => 'custom_sidebar',
        'name'          => __( 'Custom Widget', 'text_domain' ),
        'description'   => __( 'A custom widget, 'text_domain' ),
        'before_title'  => '
 
 
<h3 class="widget-title">',
        'after_title'   => '</h3>
 
 
 
',
        'before_widget' => '
 
 
<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>
 
 
 
',
    );
    register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );



/* ////////// version 2 ////////////// */

// Register Sidebars
function custom_sidebars() {

    $args = array(
        'id'            => 'custom_sidebar',
        'name'          => __( 'Custom Widget Area', 'text_domain' ),
        'description'   => __( 'A custom widget area', 'text_domain' ),
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
    );
    register_sidebar( $args );

}
add_action( 'widgets_init', 'custom_sidebars' );

/* You can now visit Appearance » Widgets page and you will see your new custom widget area. */

/* To display this sidebar or widget ready area in your theme add this code: */

/*
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('custom_sidebar') ) : ?>
<!–Default sidebar info goes here–>
<?php endif; ?>
*/

 /*------------------------------------*
CUSTOMIZE SIDEBAR FOR INDIVIDUAL POSTS
 *------------------------------------*/

/*



Customize your sidebar for individual posts
Display customized sidebar content for individual posts using custom fields. At first, find the following line of code in your single.php, index.php and page.php file.

<?php get_sidebar(); ?>

Replace it with the following code snippet.

<?php $sidebar = get_post_meta($post->ID, "sidebar", true);
get_sidebar($sidebar);
?>

When writing a post, create new custom fields named sidebar. In the value section, mention the name of the sidebar you want to display so if you built two different sidebar files (for e.g. sidebar-category.php and sidebar-promotion.php) and wanted to show the sidebar-category.php, you’d use the key as “sidebar” and value as “sidebar-category“.

*/






 /*------------------------------------*
 ADD NEW NAV MENUS
 *------------------------------------*/

/*

Add New Navigation Menus to Your Theme
WordPress allows theme developers to define navigation menus and then display them. Add this code in your theme’s functions file to define a new menu location in your theme.

You can now go to Appearance » Menus and you will see ‘My Custom Menu’ as theme location option.
https://www.wpbeginner.com/wp-themes/how-to-add-custom-navigation-menus-in-wordpress-3-0-themes/
*/

function wpb_custom_new_menu() {
  register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );

/*Now you need to add this code to your theme where you want to display navigation menu. */

/*
<?php
wp_nav_menu( array(
    'theme_location' => 'my-custom-menu',
    'container_class' => 'custom-menu-class' ) );
?>
*/







 /*------------------------------------*
DEFINE HOW POSTS ON HOMEPAGE LOOK
 *------------------------------------*/

/*

Define how individual posts should be displayed on the homepage

Most of the themes display all of your posts in the same way on the homepage. That is, on the homepage, either it shows excerpts only or it shows the full post. However, you may not want to display all of your posts in the same way on the homepage.

Find the loop in your index.php file and replace it with the following so that you can define how each post should be displayed.

<?php if (have_posts()) :

    while (have_posts()) : the_post();

         $customField = get_post_custom_values("full");

         if (isset($customField[0])) {

             //Custom field is set, display a full post

              the_title();

              the_content();

         } else {

             // No custom field set, let's display an excerpt

              the_title();

              the_excerpt();

    endwhile;

endif;

?>

In the above WordPress tips, by default excerpts are displayed on the homepage. To show posts fully in the homepage, create a custom field 'full' from the post editor and give it any value.



*/




 /*------------------------------------*
CUSTOM CSS FOR INDIVIDUAL POSTS
 *------------------------------------*/

/*

Custom CSS for individual posts
You may need to use custom stylesheet for individual posts. Insert the following code in header.php between <head>and </head>


<?php if (is_single()) {

$customstyle = get_post_meta($post->ID, 'customstyle', true);

if (!empty($customstyle)) { ?>

<style type="text/css">

<?php echo $customstyle; ?>

<style>

<?php }

} ?>

Then, add a custom field in the posts with the name customstyle and add the CSS code in there.

*/





 /*------------------------------------*
ADD CUSTOMIZABLE PAGE
 *------------------------------------*/

/*

It is possible to custom design a page with simple HTML/CSS, and installs it on your site. All that needed is to simply add the following code into the top of your custom HTML page.



After adding the code, save the page as squeeze.php and upload it to your current theme folder (../wp-content/themes/your-theme-name).
Once the file is uploaded, create a new page and choose the template Squeeze under ‘Page Attributes’. Publish the page to see it live.

*/

<?php /* Template Name: Squeeze */ ?>







 /*------------------------------------*
ADD INFINTIE SCROLL
 *------------------------------------*/

/*


Automatically load new content when the reader scrolls down and approaches the bottom of the page. Actually, infinite scroll is a Jetpack plugin feature. If you’re using a well-coded theme like the default WordPress theme, your theme will support infinite scroll.


*/

/*Install the Jetpack plugin, enable infinite scroll feature and add the following code to your functions file.*/


add_theme_support( 'infinite-scroll', array(
'container' => 'content',
'footer' => 'page',
) );








 /*------------------------------------*
DISABLE POST REVISIONS
 *------------------------------------*/

/*

Disable post revisions

‘Post revisions’ is one of the best features of WP. However, some users might not need this feature especially for those who have limited database space. This tip will enable you to save on space related to storing of revisions



*/

/*
To disable the feature, add the following code to wp-config.php file
define('AUTOSAVE_INTERVAL', 120 ); // seconds
define('WP_POST_REVISIONS', false );
This code will disable all the future revisions, and it extends the autosave interval from 60 to 120 seconds. It means your post will be autosaving every 120 seconds.
*/










 /*------------------------------------*
CLEAR POST OUT OF DATABASE
 *------------------------------------*/

/*
Delete existing post revisions
If you want not only to disable the post revision but also to delete all the existing revisions saved in your database, simply run the following SQL query from your PHPMyAdmin.
DELETE FROM wp_posts WHERE post_type = 'revision';

*/


/* simply run the following SQL query from your PHPMyAdmin.
DELETE FROM wp_posts WHERE post_type = 'revision';
*/






 /*------------------------------------*
FEATURE BOX INSIDE CONTENT
 *------------------------------------*/

/*

If you would like to add a featured box inside your post that stands out from the rest of the content, add the following code to the theme’s functions file.

Once the code is added, any text wrapped inside the shortcode will appear in a featured yellow colored box.
[yellowbox]Your featured content here[/yellowbox]



*/



function make_yellowbox($atts, $content = null) {
return '<p style="background: none repeat scroll 0 0 #ff9; clear: both; margin-bottom: 18px; overflow: hidden; border: 1px solid #e5e597; padding: 13px;">' . do_shortcode($content) . '</p>';
}
add_shortcode('yellowbox', 'make_yellowbox');







 /*------------------------------------*
POPULAR POSTS IN SIDEBAR
 *------------------------------------*/

/*

Show popular posts in the sidebar
popular posts
To show the 5 most popular posts according to the comments count, place the below lines in the sidebar.php file. Of course, if you want to show more or less than 5, just change from 5 to another value you prefer in the $result line.
*/
<h2>Popular Posts</h2>

<ul>

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");

foreach ($result as $post) {

setup_postdata($post);

$postid = $post->ID;

$title = $post->post_title;

$commentcount = $post->comment_count;

if ($commentcount != 0) { ?>

    <li><a href="/<?php echo get_permalink($postid); ?>" title="< ?php echo $title ?>">< ?php echo $title ?></a> {<?php echo $commentcount ?>}</li>

<?php } } ?></ul>



*/












 /*------------------------------------*
SHOW RELEATED POSTS
 *------------------------------------*/

/*

Show related posts
Insert the below code to single file to show related posts without a plugin.


*/


<?php  //for use in the loop, list 5 post titles related to first tag on current post

  $backup = $post;  // backup the current object
  $tags = wp_get_post_tags($post->ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i < $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]->term_id;
    }
    $args=array(
      'tag__in' => $tagIDs,
      'post__not_in' => array($post->ID),
      'showposts'=>5,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="/<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
      <?php endwhile;
    } else { ?>
      <h2>No related posts found!</h2>
    <?php }
  }
  $post = $backup;  // copy it back
  wp_reset_query(); // to use the original query again
?>
 







 /*------------------------------------*
ADD BREADCRUMBS
 *------------------------------------*/

/*

 Add breadcrumbs to your theme
To add breadcrumbs, add the following lines to your functions file.

*/



function the_breadcrumb() {

        echo '

<ul id="crumbs">';

    if (!is_home()) {

        echo '

    <li><a href="';

        echo get_option('home');

        echo '">';

        bloginfo('name');

        echo "</a></li>

";

        if (is_category() || is_single()) {

            echo '

    <li>';

            the_category('title_li=');

            if (is_single()) {

                echo "</li>

    <li>";

                the_title();

                echo '</li>

';

            }

        } elseif (is_page()) {

            echo '

    <li>';

            echo the_title();

            echo '</li>

';

        }

    }

    elseif (is_tag()) {single_tag_title();}

    elseif (is_day()) {echo"

    <li>Archive for "; the_time('F jS, Y'); echo'</li>

';}

    elseif (is_month()) {echo"

    <li>Archive for "; the_time('F, Y'); echo'</li>

';}

    elseif (is_year()) {echo"

    <li>Archive for "; the_time('Y'); echo'</li>

';}

    elseif (is_author()) {echo"

    <li>Author Archive"; echo'</li>

';}

    elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "

    <li>Blog Archives"; echo'</li>

';}

    elseif (is_search()) {echo"

    <li>Search Results"; echo'</li>

';}

 

    echo '</ul>

';

}

/*
After that, add this line of code to your theme’s template where you would like the breadcrumbs to show up (for e.g. single.php, archives.php, etc.).

< ?php the_breadcrumb(); ?>

*/









 /*------------------------------------*
LINK TO EXTRENAL LINKS FROM POST TITLES
 *------------------------------------*/

/*

Link to external links from your post titles
Usually, titles of blog posts in homepage are linked to the original post URL.

However, if the sole purpose of publishing a particular blog post is to share a particular external link, you may not want to entice the users to open up your post. Instead, the users can visit the external link by simply clicking the blog post title from the homepage itself. Add the following code to functions.php file.

function print_post_title() {

global $post;

$thePostID = $post->ID;

$post_id = get_post($thePostID);

$title = $post_id->post_title;

$perm = get_permalink($post_id);

$post_keys = array(); $post_val = array();

$post_keys = get_post_custom_keys($thePostID);

if (!empty($post_keys)) {

foreach ($post_keys as $pkey) {

if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {

$post_val = get_post_custom_values($pkey);

}

}

if (empty($post_val)) {

$link = $perm;

} else {

$link = $post_val[0];

}

} else {

$link = $perm;

}

echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';

}

Then, open your index.php and find the following code

<h2><a href="/<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

Replace it with the below code and you’re done!

<h2><a href="/<?php the_permalink() ?>" rel="bookmark"><?php print_post_title() ?></a></h2>



*/




 /*------------------------------------*
MAKE FEATURE IMAGES REQUIRED TO POST
 *------------------------------------*/

/*

Make featured image required for publishing a blog post

Tweak your functions file to make featured images mandatory for publishing blog posts.
*/

add_action('save_post', 'wpds_check_thumbnail');

add_action('admin_notices', 'wpds_thumbnail_error');

 

function wpds_check_thumbnail( $post_id ) {

 // change to any custom post type 

  if( get_post_type($post_id) != 'post' )

      return;

 

  if ( ! has_post_thumbnail( $post_id ) ) {

   // set a transient to show the users an admin message

    set_transient( "has_post_thumbnail", "no" );

   // unhook this function so it doesn't loop infinitely

    remove_action('save_post', 'wpds_check_thumbnail');

   // update the post set it to draft

    wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));

 

    add_action('save_post', 'wpds_check_thumbnail');

  } else {

    delete_transient( "has_post_thumbnail" );

  }

}

 

function wpds_thumbnail_error() {

 // check if the transient is set, and display the error message

  if ( get_transient( "has_post_thumbnail" ) == "no" ) {

    echo "<div id='message' class='error'><p><strong>You must add a Featured Image before publishing this. Don't panic, your post is saved.</strong></p></div>";

    delete_transient( "has_post_thumbnail" );

  }

}





 /*------------------------------------*
ADD CONFIRM TO POST BOX
 *------------------------------------*/

/*

dd confirmation box when publishing pages & posts
This trick avoids publishing an incomplete post by accident. You can add the following snippet, as usual to the functions file.

*/

add_action( 'admin_print_footer_scripts', 'sr_publish_molly_guard' );

function sr_publish_molly_guard() {

echo "

<script>

jQuery(document).ready(function($){

$('#publishing-action input[name="publish"]').click(function() {

if(confirm('Are you sure you want to publish this?')) {

return true;

} else {

$('#publishing-action .spinner').hide();

$('#publishing-action img').hide();

$(this).removeClass('button-primary-disabled');

return false;

}

});

});

</script>

";

}





 /*------------------------------------*
SET POST EXPIRATION DATE
 *------------------------------------*/

/*


60. Set expiration date for your posts
Setting an expiration date for your post is a good idea especially if you are offering limited time discount for your products or running a contest.
Just replace the WP loop with the following code.

<?php

if (have_posts()) :

while (have_posts()) : the_post(); ?>

$expirationtime = get_post_custom_values('expiration');

if (is_array($expirationtime)) {

$expirestring = implode($expirationtime);

}

$secondsbetween = strtotime($expirestring)-time();

if ( $secondsbetween > 0 ) {

// For example…

the_title();

the_excerpt();

}

endwhile;

endif;

?>

In the custom fields, make sure you choose key ‘expiration’ and the following date format: mm/dd/yyyy 00:00:00. This WordPress trick doesn’t remove or unpublish the post but it excludes the post from being displayed in the loop. For SEO purposes, you'll want to unpublish this post if it no longer relevant, because it will still appear in such files as the sitemap.xml of your site.

*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/





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

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

/*



*/










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

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

/*



*/









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

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

/*



*/











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

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

/*



*/









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

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

/*



*/











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

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

/*



*/









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

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

/*



*/











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

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

/*



*/









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

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

/*



*/



















Scroll to Top