Functions File – basic adds –

[code style=”php”]

/*/// must have plugins ///*/

https://www.wpbeginner.com/showcase/24-must-have-wordpress-plugins-for-business-websites/

/*//// also has pre-included and registered js ////*/

Default Scripts Included and Registered by WordPress #Default Scripts Included and Registered by WordPress
By default, WordPress includes many popular scripts commonly used by web developers, as well as the scripts used by WordPress itself.

MAKE A CHILD THEME – plugin-

/*/// no code snippets ////*/

add_action( ‘wp_head’, function() {
?>
Your stuff in here.
<?php
} );

/*/// create plugins – no code snippets /////*/

Write Documentation
https://www.phpdoc.org/

Plugin Creator Plugin

/*//// combining function ////*/

function add_theme_scripts() {
wp_enqueue_style( ‘style’, get_stylesheet_uri() );

wp_enqueue_style( ‘slider’, get_template_directory_uri() . ‘/css/slider.css’, array(), ‘1.1’, ‘all’);

wp_enqueue_script( ‘script’, get_template_directory_uri() . ‘/js/script.js’, array ( ‘jquery’ ), 1.1, true);

if ( is_singular() && comments_open() && get_option( ‘thread_comments’ ) ) {
wp_enqueue_script( ‘comment-reply’ );
}
}
add_action( ‘wp_enqueue_scripts’, ‘add_theme_scripts’ );

/*/// enable shortcodes in text 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’);

/*//// remove default linking on images adds none
https://www.wpbeginner.com/wp-tutorials/automatically-remove-default-image-links-wordpress/
///////*/

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);

/*///// custom logo to dashboard 16×16 px /////*/

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 welcome panel //////*/

remove_action(‘welcome_panel’, ‘wp_welcome_panel’);

/*///// show total # users //////*/

// Function to return user count
function wpb_user_count() {
$usercount = count_users();
$result = $usercount[‘total_users’];
return $result;
}
// Creating a shortcode to display user count
add_shortcode(‘user_count’, ‘wpb_user_count’);

/*///// add even odd css classes to posts
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’;

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

/*///// add additional mime types to wp (svg and psd)
https://www.wpbeginner.com/wp-tutorials/how-to-add-additional-file-types-to-be-uploaded-in-wordpress/
/////*/

function my_myme_types($mime_types){
$mime_types[‘svg’] = ‘image/svg+xml’; //Adding svg extension
$mime_types[‘psd’] = ‘image/vnd.adobe.photoshop’; //Adding photoshop files
return $mime_types;
}
add_filter(‘upload_mimes’, ‘my_myme_types’, 1, 1);

/*//////// change footer in admin panel ////////*/

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 dashboard widget /////////*/

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>’;

/*//////// change gravatar – update – then select on settings => discussion ////*/

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 $

/*/////// dynamic copyright – once set up – add –
<?php echo wpb_copyright(); ?>
in footer
///////*/

unction 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;
}

/*// https://www.wpbeginner.com/wp-tutorials/how-to-create-additional-image-sizes-in-wordpress //*/
/*///////// add image sizes ////////////*/

add_image_size( ‘sidebar-thumb’, 120, 120, true ); // Hard Crop Mode
add_image_size( ‘homepage-thumb’, 220, 180 ); // Soft Crop Mode
add_image_size( ‘singlepost-thumb’, 590, 9999 ); // Unlimited Height Mode

/*// to display – <?php the_post_thumbnail( ‘homepage-thumb’ ); ?> add to code /////*/

/*//// add menu slot in site ////*/

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

/*/ fo to menu area – set it up /*/

/*/// add to theme – where you want menu displayed //*/

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

/*//// add extra info for authors /////*/
/*// 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)

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

/*// adding widget ready aras in sidebars /////*/

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

/*//// add to theme – to dislay widget area ///////*/

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

/*//// add featured images to rss feeds ////*/
/*// https://www.wpbeginner.com/wp-tutorials/how-to-add-post-thumbnail-to-your-wordpress-rss-feeds// //*/

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = ‘<p>’ . get_the_post_thumbnail($post->ID) .
‘</p>’ . get_the_content();
}
return $content;
}
add_filter(‘the_excerpt_rss’, ‘rss_post_thumbnail’);
add_filter(‘the_content_feed’, ‘rss_post_thumbnail’);

/*//// exclude category from rss /////*/

function exclude_category($query) {
if ( $query->is_feed ) {
$query->set(‘cat’, ‘-5, -2, -3’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);

/*//// change login errors /////*/

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

/*/// disable search ////*/

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;" ) );

/*//// delay posts from publishing for set time ///*/

function publish_later_on_feed($where) {

global $wpdb;

if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);

// value for wait; + device
$wait = ’10’; // integer

// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

// add SQL-sytax to default $where
$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait ";
}
return $where;
}

add_filter(‘posts_where’, ‘publish_later_on_feed’);

/*//// change read more to excerpt /////*/

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 //// */

function new_excerpt_length($length) {
return 100;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);

/*/// ADD ADMIN USER WITH FTP (maybe make emergency plugin) – in functions /////*/

function wpb_admin_account(){
$user = ‘Username’;
$pass = ‘Password’;
$email = ’email@domain.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’);

/*///// disable rss feeds /////*/

function fb_disable_feed() {
wp_die( __(‘No feed available,please visit our <a href="’. get_bloginfo(‘url’) .’">homepage</a>!’) );
}

add_action(‘do_feed’, ‘fb_disable_feed’, 1);
add_action(‘do_feed_rdf’, ‘fb_disable_feed’, 1);
add_action(‘do_feed_rss’, ‘fb_disable_feed’, 1);
add_action(‘do_feed_rss2’, ‘fb_disable_feed’, 1);
add_action(‘do_feed_atom’, ‘fb_disable_feed’, 1);

/*/// Add an Author Info Box in WordPress Posts – see
https://www.wpbeginner.com/wp-tutorials/25-extremely-useful-tricks-for-the-wordpress-functions-file/
////*/

/*/// disable xml-rpc
https://www.wpbeginner.com/plugins/how-to-disable-xml-rpc-in-wordpress/
////*/

add_filter(‘xmlrpc_enabled’, ‘__return_false’);

/*/// link feat images to posts ////*/

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 );

/*///// Category Exlcluder /////////////////*/

/*/// Exclude multiple page ///////*/

function remove_my_categories( $wp_query ) {
// 61 = Daily Tweets, 74 = Testing
$remove_cat = ‘-61,-74’;

// remove from archives (except category archives), feeds, search, and home page, but not admin areas
if( (is_home() || is_feed() || is_search() || ( is_archive() && !is_category() )) &&; !is_admin()) {
set_query_var(‘cat’, $remove_cat);
//which is merely the more elegant way to write:
//$wp_query->;set(‘cat’, ‘-‘ . $remove_cat);
}
}

add_action(‘pre_get_posts’, ‘remove_my_categories’ );

/*/// exclude from feeds ////*/

function myFeedExcluder($query) {

if ($query->is_feed) {

$query->set(‘cat’,’-12′);

}

return $query;

}

add_filter(‘pre_get_posts’,’myFeedExcluder’);

/*/// exclude from LOOP ////*/

/*// place befor loop use category # ///*/

<?php query_posts(‘showposts=2&cat=-52’); ?>

/*///// from homepage ////*/

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-5’ );
}
return $query;
}

add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );

/*/// multiple cats ////*/

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-5, -9, -23’ );
}
return $query;
}

add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );

[/code]

Scroll to Top