Skip to content

Instantly share code, notes, and snippets.

@antcms
antcms / wp-editor-user-access.php
Last active June 16, 2016 02:38
WordPress Editor can add and delete users, but not Admin level (for functions.php)
<?php
// ADD USER PERMISIONS FOR EDITOR
function add_theme_caps() {
$role = get_role( 'editor' );
$role->add_cap( 'edit_users' );
$role->add_cap( 'delete_users' );
}
add_action( 'admin_init', 'add_theme_caps');
// EDITOR CAN'T DELETE ADMIN USERS
@antcms
antcms / wp_auto_page_children.php
Last active June 16, 2016 02:39
Automatically insert WordPress child pages (and bbPress Forum) when publishing a page / post / custom post type (for functions.php)
<?php
//AUTO ADD WORDPRESS POST CHILDREN
function add_page_children( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_is_post_revision( $post_id )
&& 'page' == get_post_type( $post_id ) // Change as per required post type
&& 'auto-draft' != get_post_status( $post_id ) ) {
$thispage = get_post( $post_id );
$ptitle = $thispage->post_title; // Set the Parent Page title as a variable to add to new pages
@antcms
antcms / wp-nav-menu-no-div-no-ul.php
Created June 16, 2016 02:50
Remove DIV and UL from WordPress wp_nav_menu(). This example replaces the registered menu 'header-menu' and replaces 'wp_nav_menu( array( 'theme_location' => 'header-menu' ) ) ;' with the function 'wp_nav_menu_head();' in your theme file. (For functions.php)
<?php
//REMOVE DIV and UL FROM WP_NAV_MENU
function wp_nav_menu_header(){
$options = array(
'echo' => false,
'container' => false,
'theme_location' => 'header-menu',
'fallback_cb'=> 'fall_back_menu'
);
@antcms
antcms / wp-display-cpt-by-term.php
Created June 16, 2016 03:48
Sort and display WordPress custom post types by taxonomy on Archive or Page
<?php get_header(); ?>
<h1><?php post_type_archive_title(); ?></h1>
<!-- Set Up Page Anchors -->
<div>
<ul>
<li>Jump to:</li>
<?php
$custom_terms = get_terms('termname'); // Taxonomy
foreach($custom_terms as $custom_term) {
$anchor = $custom_term->slug;
@antcms
antcms / wp-page-has-children-conditional.php
Last active June 16, 2016 03:49
Change the WordPress Template for page.php if it has children
<?php get_header(); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
$pid = get_the_ID();
$args = array(
'post_parent' => $pid,
'post_type' => 'page', // Change as per post type
'numberposts' => -1,
'post_status' => 'publish'
);
@antcms
antcms / wp-post-thumbnail-after-paragraph.php
Created June 16, 2016 04:00
Display the Featured Image after the first paragraph in WordPress
<?php get_header(); ?>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php
$after_p_img = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $after_p_img){
$contents = explode("</p>", $content);
$p_count = 1;
@antcms
antcms / wordpress-remove-quick-edit-categories.php
Created July 12, 2016 10:05
Remove the Category menu for Authors in the WordPress Quick Edit Menu
//REMOVE TAX IN QUICK EDITOR
add_action('admin_head', 'no_tax_display');
function no_tax_display(){
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
echo '<style>
.inline-edit-categories, #acf-form-data {
display: none;
}
</style>';
@antcms
antcms / wordpress-logout-redirect.php
Last active July 12, 2016 10:05
Logout redirect for front-end users in WordPress, in this case Authors with access to the back end to add posts. Redirects to a front end page called 'Members Home'.
//AUTHOR LOGOUT
add_filter( 'logout_url', 'author_logout_page', 10, 2 );
function author_logout_page( $logout_url, $redirect ) {
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
return home_url( '/members-home/' );
}
}
@antcms
antcms / force-style.php
Last active May 11, 2017 01:02
Force Wordpress Styles to Update
//ADD THIS TO YOUR functions.php FILE
//REMOVES VERSION NUMBERS AND REPLACES WITH A RANDOM NUMBER SO STYLE SHEET RELOADS EVERY TIME
function remove_replace_css_ver( $src ) {
if ( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
$newsrc = $src . '?ver=' . rand(111,9999);
return $newsrc;
}
add_filter( 'style_loader_src', 'remove_replace_css_ver', 9999 );
@antcms
antcms / wp-simple-membership-dynamic-register.php
Last active September 30, 2019 08:47
Dynamic Registration form for WordPress Simple Membership Plugin (with Form Builder Addon). Choose Membership level from a Select Menu and div below dynamically populates with the correct form for that level. Requires jQuery. https://simple-membership-plugin.com / https://simple-membership-plugin.com/simple-membership-form-builder-addon/
<div class="registration">
<h2 class="title">Register Here:</h2>
<script type="text/javascript">
$(document).ready(function(){
var hideAll = function() {
$('div[class^=option]').hide();
}
$('#select').on('change', function() {
hideAll();
var category = $(this).val();