Add this method to the function.php file. It will append a stylesheet file in the admin area of WordPress so you can add your custom CSS in the editor. Don't forget to update path to the file.
Register in the functions.php to add a new autogenerated image size for you posts and pages. Use name, width and height attributes to define the new size. By passing 9999 as a value of width or height the image will retain its original size for that dimension.
/**
* Register post meta data in REST
* Use rest_api_init to register post's meta data
*/add_action('rest_api_init','add_post_meta_to_rest');functionadd_post_meta_to_rest(){$post_type='post';// You can use taxonomy or a custom post name.$rest_name='post-meta'// All meta fields will be stored under this REST attribute.register_rest_field($post_type,$rest_name,array('get_callback'=>function($object){returnget_post_meta($object['id']);},'schema'=>null,));}
Use add_shortcode function to register shortcode for later use. The first parameter is a name of your shortcode, the second is a callback function where you can add the content to return.
functionwpshort_assemble_shortcode(){$message='Hello from the shortcode!';return$message;}/*
* Register shortcode
*/add_shortcode('my_shortcode','wpshort_assemble_shortcode');/*
* Use [my_shortcode] handle in a WYSIWYG editor to display it.
* Alternatively, you can use do_shortcode() function directly in code.
*/
Use this filter to add a slug-based css class to each menu item.
#wordpress#php#menu
functionadd_menu_item_slug_class($classes,$item,$args){$item_class=sanitize_title($item->title);// Convert title to a slug$classes[]="menu-item-".$item_class;return$classes;}add_filter('nav_menu_css_class','add_menu_item_slug_class',1,3);
bloginfo function prints information about your site. Pass an argument to return name, description, wpurl, url, admin_email, charset, version, html_type, text_direction, language, stylesheet_url, stylesheet_directory, template_url, pingback_url and more.
When registering a new custom post type you can encounter a bug where a single custom post returns a 404 page even when a valid page template exists. To fix it add the following snippet to the functions.php file. Make sure to remove it once the bug is fixed as you do not want it to run with each page load.
Pass arguments to WP_Query with a tag of a custom post.
#wordpress#php
<?php// args$args=array('post_type'=>'custom-post','order'=>'ASC','posts_per_page'=>-1// Get all posts);$the_query=newWP_Query($args);?><?phpif($the_query->have_posts()):?><?phpwhile($the_query->have_posts()):$the_query->the_post();?><ahref="<?phpthe_permalink();?>"><?phpthe_title();?></a><?phpendwhile;?><?phpendif;?><?phpwp_reset_query();// Reset loop ?>
Disables Gutenberg editor and brings back the Classic editor. Additional actions remove related styles too.
#wordpress#php
/**
* Disable Gutenberg on the back end.
*/add_filter('use_block_editor_for_post','__return_false');/**
* Disable Gutenberg for widgets.
*/add_filter('use_widgets_blog_editor','__return_false');/**
* Disable Gutenberg styles.
*/add_action('wp_enqueue_scripts',function(){// Remove CSS on the front end.wp_dequeue_style('wp-block-library');// Remove Gutenberg theme.wp_dequeue_style('wp-block-library-theme');// Remove inline global CSS on the front end.wp_dequeue_style('global-styles');},20);
In the root directory open the wp-config.php file and add the following code before the "That's all, stop editing! Happy publishing" comment. It will disable Theme Editor in the Admin area located in Appearance section. To enable it again, change the Bool attribute to false.
In the root directory open the wp-config.php file and add the following code before the "That's all, stop editing! Happy publishing" comment. It will disable Theme Editor in the Admin area located in Appearance section and the options to update Themes and Plugins in the CMS. To enable it again, change the Bool attribute to false.
In the root directory open the wp-config.php file and add the following code before the "That's all, stop editing! Happy publishing" comment. Next, login to your WordPress admin panel and go to Tools > Network Setup to configure it.
You can enable post and page revisions by adding the following line before the "That's all, stop editing! Happy publishing" comment in the wp-config.php file. If the attribute is set to 'true' WordPress will store all revisions for the posts. Setting it to 'false' will deactivate revisions altogether. Finally, you can pass a positive integer to set the maximum number of stored revisions.
Enqueue WordPress dashicons to frontend. By default dashicons are only available in the admin area. Add the following code to the functions.php file to use WordPress icons anywhere in your website.
Using get_bloginfo lets you access information about your site. Pass an argument to return name, description, wpurl, url, admin_email, charset, version, html_type, text_direction, language, stylesheet_url, stylesheet_directory, template_url, pingback_url and more.
get_post returns an object holding information about the post. By passing an argument you can access object arguments and return the data. Available data - ID, post_author, post_date,post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type and comment_count.
The following methods allow for getting and displaying translated text in your theme.
#wordpress#php
// Get text__('Your text','text_domain');// Get text with a specified context_x('Your text','Form label','text_domain');// Echo out text_e('Your text','text_domain');// Use translated text inside of sprintf methodechosprintf('This is %s in a paragraph',__('your text','text_domain'));
Helper function for displaying the post thumbnail. Default Wordpress image sizes - thumbnail|medium|medium_large|large|full. Default WooCommerce image types - shop_thumbnail|shop_catalog|shop_single.
Lets you register a script in the functions.php file to be called from another part of your site. For example you can register a javascript file for the gallery component and only enqueue it in the page if the gallery component has been used.
#wordpress
// Register script in functions.php filefunctionregister_gallery_script(){wp_register_script('galleryjs',get_stylesheet_directory_uri().'/path/to/gallery.js',array(),'1.0',true);}add_action('wp_enqueue_scripts','register_gallery_script');// Call the script from the componentwp_enqueue_script('galleryjs');
Set a global variable as part of the functions.php file.
#wordpress#php
// Add this function in function.php filefunctionset_some_global_variable(){global$some_variable;$some_variable=1;}add_action('after_setup_theme','set_some_global_variable');// Than, it can be used in other parts of the themeglobal$some_variable;echo$some_variable;
This handy functions will append a trailing slash to a given string. First it strips all slashes from the end of the url and then adds one to avoid double-slashing.