Skip to content

Instantly share code, notes, and snippets.

@SteveJonesDev
SteveJonesDev / functions.php
Created May 9, 2018 18:41
How to insert custom code into the WordPress post content
<?php // dont include this line
//Insert code after second paragraph of single post content.
add_filter( 'the_content', 'sj_insert_code' );
function sj_insert_code( $content ) {
$code = '<div>Code to be inserted goes here</div>';
if ( is_single() && ! is_admin() ) {
return sj_insert_after_paragraph( $code, 2, $content );
}
return $content;
@SteveJonesDev
SteveJonesDev / functions.php
Last active February 7, 2017 03:27
Wordpress WP_User_Query - Query by meta with random ordering
<?php
/*
WP_User_Query orderby random
put this in functions.php
*/
add_filter('pre_user_query', function(&$query) {
if($query->query_vars["orderby"] == 'rand') {
$query->query_orderby = 'ORDER by RAND()';
}
});
@SteveJonesDev
SteveJonesDev / gist:c67e673b5f0828b4b738d5f4581c0005
Created January 25, 2017 03:46 — forked from markoheijnen/gist:2407319
Get WordPress menu title by theme location
<?
function get_menu_title( $theme_location, $default_name = 'menu' ) {
if ( $theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
if( $menu && $menu->name ) {
return $menu->name;
}
}