Functions - martindubenet/Wordpress GitHub Wiki

Home  ][  Plugins  ][  Functions  ][  MySQL setup  ]

Common Functions

Function Description Example
bloginfo() Displays information about the current site depending on what parameter you pass it.
get_template_directory_uri() Prints the complete path of the (parent only) theme. echo( get_template_directory_uri() . 'css/parent-style.css' )
get_stylesheet_directory_uri() Prints the complete path of the actual (child) theme. echo( get_stylesheet_directory_uri() . 'css/child-style.css' )
language_attributes() Prints the language selected in the Dashboard Settings <html <?php language_attributes(); ?> >
register_nav_menus() Prints an unordered list of the navigation that is designed via the wp_nav_menu feature from the Dashboard's Style/Menus. <?php wp_nav_menu( array( 'theme_location' => 'header-main-nav', 'container' => 'section', 'menu_id' => 'headerMainMenu', 'menu_class' => 'collapse navbar-collapse', 'walker' => new wp_bootstrap_navwalker() ) ); ?>
get_the_excerpt() Display the post excerpt. <meta name="description" content="<?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?>" />

For theme developers

Get Header, Footer and Sidebar within your template page-example.php

<?php
/**
 * Template Name: Example
 */
get_header();
?>
…
<?php
get_sidebar();
get_footer();
?>

Echo translation ready strings of text

Very useful for short strings used as, for example, title attributes that you want to get translated by WPML later on.

<?php esc_html_e( 'Skip to content', 'themeUI nav' ); ?>

Detect if this is a child theme

If this theme's URI is not equal to parent theme's URI, then We are in a child theme.

<?php
if ( get_stylesheet_directory_uri() === get_stylesheet_directory_uri() ) :

    echo ( 'We are in a child theme' );

endif;
?>

Enable SVG image in Wordpress

Requirements:

  1. XML instructions <?xml version="1.0" encoding="utf-8"?> :_Your SVG image requires to contains the XML declaration on the first line other wize it won't pass Wordpress file validation_,
  2. CSS width & height :_SVG images will require dimensions attributes to be declared with CSS other wise the image is 0x0 making it invisible to users_.
/**
 * Enable SVG file upload to Wordpress Medias
 * @link https://css-tricks.com/snippets/wordpress/allow-svg-through-wordpress-media-uploader/
 */
function cc_mime_types($mimes) {
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');