Dictonary: file functions.php

File functions.php is file located inside your theme or your child theme. If this file not exist in your child theme – you can create this file.

Remember: When you has an error in your functions.php –  the frontend of your website couldn’t working.

How to add PHP to wordpress text widget

When you need, you can add to the textwidget php script. In standard version, this not work,  but after added simple code in your child theme in file: functions.php, your php code will be executed.

function uniset_php_execute($html){

    if(strpos($html,"<"."?php")!==false){ 
        ob_start(); eval("?".">".$html);
        $html=ob_get_contents();
        ob_end_clean();
    }
    return $html;
}
add_filter('widget_text','uniset_php_execute',99);

 

After that, In your textwidget add simple script, to try it. For example:

<?php
echo "Some random number: <pre>".rand()."</pre>";
?>

NOTICE: For security reasons this code is not recommended, you should not use this code on production website, but sometimes it is usable for programmers to display environment variables or for other programming needs: we can check your cache and some other reasons.

Shortcode in the text widget – in the sidebar.

In few my works i need use shortcode inner text widget. When you need add custom gallery or other shortcodes you can add this code in your functions.php:

<?php
/* Allow shortcodes in widget areas */
add_filter('widget_text', 'do_shortcode');
?>

Simple? Please share this post to your friends.

How to automatic remove featured image when deleting the post

When you need autoremove featured image from the post – when the post will be deleted, please add code:

<?php 
add_action( 'before_delete_post', 'arkadiuszbartnik_remove_attachment_with_post', 10 );
function arkadiuszbartnik_remove_attachment_with_post($post_id)
{
        if(has_post_thumbnail( $post_id ))
        {
          $attachment_id = get_post_thumbnail_id( $post_id );
          wp_delete_attachment($attachment_id, true);
        }
}