Last active
August 12, 2022 09:29
-
-
Save devonmather/5761709 to your computer and use it in GitHub Desktop.
Various common Wordpress Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Load in WP functions outside of wordpress | |
define('WP_USE_THEMES', false); | |
require('/home/kindyhub/public_html/wp-blog-header.php'); ?> | |
add_action('wp_enqueue_scripts', 'no_more_jquery'); | |
function no_more_jquery(){ | |
//if(!is_bbpress()) { //sample condition inside function | |
wp_deregister_script('jquery'); | |
//} | |
} | |
$args = array( | |
'meta_key' => '_wp_page_template', | |
'meta_value' => 'pages-yachts.php', | |
'hierarchical' => 0, | |
); | |
$pages = get_pages($args); | |
<?php $headerImages = get_uploaded_header_images(); | |
$headerImages = shuffle($headerImages); //randomize | |
foreach($headerImages as $headerImage) { | |
print_r($headerImage); ?> | |
<?php } ?> | |
if(is_user_logged_in() && current_user_can('manage_options')){ | |
// user is logged in && admin | |
} | |
else { | |
//not admin or logged in | |
header('Location: ' . get_site_url()); | |
exit; | |
} | |
is_page_template('pages-listings-property.php'); | |
get_page_template_slug($page->ID) == 'pages-news.php'); | |
//get page ids based on templates used | |
$the_query = new WP_Query(array( | |
'post_type' => 'page', | |
'meta_key' => '_wp_page_template', | |
'meta_value' => 'templates/_partner.php' | |
)); | |
//add menu item under appearance for editors | |
$role_object = get_role( 'editor' ); | |
$role_object->add_cap( 'edit_theme_options' ); | |
if (!current_user_can('activate_plugins')) { | |
function hide_menu() { | |
remove_submenu_page( 'themes.php', 'themes.php' ); // hide the theme selection submenu | |
remove_submenu_page( 'themes.php', 'widgets.php' ); // hide the widgets submenu | |
remove_submenu_page( 'themes.php', 'customize.php' ); // hide the widgets submenu | |
remove_submenu_page('themes.php', 'nav-menus.php'); // hide the menus submenu | |
} | |
add_action('admin_head', 'hide_menu'); | |
} | |
post_password_required( $post ); | |
if(is_page('private-media') || in_category('private-media')) { | |
echo '<meta name="robots" content="noindex, nofollow" />'; | |
} | |
global $post; | |
if ($post->post_parent && post_password_required($post->post_parent)) { | |
wp_redirect(get_permalink($post->post_parent)); | |
} | |
//better way to include template files with variables in scope in parent or child theme | |
include(locate_template('your-template-name.php')); | |
//format post_content | |
$page_id = 4; | |
$page = get_page($page_id); | |
echo apply_filters('the_content', $page->post_content); | |
// remove images from post_content | |
$content = get_the_content(); | |
$postOutput = preg_replace('/<img[^>]+./','', $content); | |
<?php $args = array( | |
'posts_per_page' => -1, | |
'orderby' => 'menu_order', | |
'order' => 'ASC', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'post_parent' => $post->ID, | |
); | |
$images = get_posts($args); | |
foreach ($images as $image) { | |
if($image->post_title == 'feature'){ | |
list($url, $width, $height) = wp_get_attachment_image_src($image->ID, 'thumbnail'); ?> | |
<img src="<?php echo $url; ?>" alt="<?php echo $post->post_title; ?>"> | |
<?php } | |
} ?> | |
<?php $args = array( | |
'post_type' => 'offer', | |
'paged' => $paged, | |
); | |
$offerQuery = new WP_Query($args); | |
if($offerQuery->have_posts()) { ?> | |
<div class="offersContainer articleContainer"> | |
<?php while ( $offerQuery->have_posts() ) : $offerQuery->the_post(); include(locate_template('includes/includes-post.php')); | |
endwhile; | |
if ($offerQuery->max_num_pages > 1) { ?> | |
<div class="navBelow navigation"> | |
<div class="pageNav navPrevious animate"><?php next_posts_link('Older'); ?></div> | |
<div class="pageNav navNext animate"><?php previous_posts_link('Newer'); ?></div> | |
</div><!--END NavBelow--> | |
<?php } | |
wp_reset_postdata(); ?> | |
</div><!--END BlogContainer--> | |
<?php } ?> | |
//remove admin bar for all but admin/editors | |
add_action('after_setup_theme', 'remove_admin_bar'); | |
function remove_admin_bar() { | |
if (!current_user_can('edit_pages')) { | |
add_filter('show_admin_bar', '__return_false'); | |
} | |
} | |
// record page load and queries | |
add_action( 'wp_footer', 'tcb_note_server_side_page_speed' ); | |
function tcb_note_server_side_page_speed() { | |
date_default_timezone_set( get_option( 'timezone_string' ) ); | |
$content = '[ ' . date( 'Y-m-d H:i:s T' ) . ' ] '; | |
$content .= 'Page created in '; | |
$content .= timer_stop( $display = 0, $precision = 2 ); | |
$content .= ' seconds from '; | |
$content .= get_num_queries(); | |
$content .= ' queries'; | |
if( ! current_user_can( 'administrator' ) ) $content = "<!-- $content -->"; | |
echo $content; | |
} | |
/** | |
* Restricts certain menu items from appearing the WP Admin area. Does not | |
* affect Administrator users. | |
* | |
* @action admin_menu | |
*/ | |
function my_restrict_admin_menu_items() { | |
// Don't restrict Administrator users. | |
if(current_user_can( 'manage_options')){ | |
return; | |
} | |
// Array of the menu item slugs you want to remove. | |
$restricted = array( | |
'menu-comments', // Comments (slug is ID of nav item) | |
'menu-tools', // Tools(slug is ID of nav item) | |
); | |
global $menu; | |
foreach ($menu as $item => $data) { | |
if (!isset($data[5])) { | |
continue; // Move along if the current $item doesn't have a slug. | |
} | |
elseif (in_array( $data[5], $restricted)) { | |
unset($menu[$item]); // Remove the current $item from the $menu. | |
} | |
} | |
} | |
add_action('admin_menu', 'my_restrict_admin_menu_items'); | |
//redirect page within wp-admin | |
function feature_bookings_preprocess_pages($value){ | |
global $pagenow; | |
$page = (isset($_REQUEST['page']) ? $_REQUEST['page'] : false); | |
if($pagenow == 'admin.php' && $page == 'feature-bookings'){ | |
wp_redirect('http://google.com'); | |
exit; | |
} | |
} | |
add_action('admin_init', 'feature_bookings_preprocess_pages'); | |
/*-------------------------------------------------------------------------------------*/ | |
/* Login Hooks and Filters for custom login redirects | |
/*-------------------------------------------------------------------------------------*/ | |
if( ! function_exists( 'custom_login_fail' ) ) { | |
function custom_login_fail( $username ) { | |
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from? | |
// if there's a valid referrer, and it's not the default log-in screen | |
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) { | |
if ( !strstr($referrer,'?login=failed') ) { // make sure we don’t append twice | |
wp_redirect( $referrer . '?login=failed' ); // append some information (login=failed) to the URL for the theme to use | |
} else { | |
wp_redirect( $referrer ); | |
} | |
exit; | |
} | |
} | |
} | |
add_action( 'wp_login_failed', 'custom_login_fail' ); // hook failed login | |
if( ! function_exists( 'custom_login_empty' ) ) { | |
function custom_login_empty(){ | |
$referrer = $_SERVER['HTTP_REFERER']; | |
if ( strstr($referrer,get_home_url()) && $user==null ) { // mylogin is the name of the loginpage. | |
if ( !strstr($referrer,'?login=empty') ) { // prevent appending twice | |
wp_redirect( $referrer . '?login=empty' ); | |
} else { | |
wp_redirect( $referrer ); | |
} | |
} | |
} | |
} | |
add_action( 'authenticate', 'custom_login_empty'); | |
//embed video using wordpress player | |
global $wp_embed; | |
/** | |
* Replace the global $post with a fake ID (a really crazy one!) | |
* or else WP_Embed::autoembed() will skip the oEmbed check | |
* for some reason. | |
**/ | |
global $post; | |
$oldpost = $post; | |
$post = new stdClass(); | |
$post->ID = PHP_INT_MAX; | |
// Process oEmbeds | |
echo $wp_embed->autoembed( do_shortcode('https://www.youtube.com/watch?v=waYqBVqZQEU') ); | |
// Restore the original $post | |
$post = $oldpost; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm trying your snippet //add menu item under appearance for editors
I added it to my theme's functions.php file but it doesn't work.
Any clue what can go wrong?
Thanks