Skip to content

Instantly share code, notes, and snippets.

@MogulChris
MogulChris / newadmin.php
Last active November 25, 2019 21:13
Create a WordPress administrator user via FTP
<?php
//Need WordPress access but only have FTP access?
//This snippet will let you create an administrator user, as long as you can get it onto the server in the root of the site
//Then visit https://www.site.com/newadmin.php
//Finally, DELETE THE SCRIPT when you have gained access.
require_once('wp-load.php');
$userdata = array(
'user_login' => 'newadmin',
'user_email' => '[email protected]',
@MogulChris
MogulChris / mymodule.module.php
Created February 11, 2020 22:58
Optgroups in Drupal 7 select fields with the Form API
<?php
//Wow I don't miss working on Drupal. Anyway, I needed to do so recently and this took a little Googling to figure out.
//Drupal 7 select fields can have optgroups if you make their options an array of arrays, keyed by the optgroup names
function mymodule_form_alter(&$form, &$form_state, $form_id){
//assuming we are trying to alter the form of a custom node type called event
if ($form_id == 'event_node_form') {
@MogulChris
MogulChris / functions.php
Created February 24, 2020 00:45
Scheduling a Wordpress Cron Job (with custom schedule)
<?php
//This is how to set up a cron job via PHP in WordPress.
//Do not rely on simply adding it via a plugin like WP Crontrol, as WordPress occasionally loses cron jobs or fails to reschedule them.
//1. Add the function you want to run regularly, and an action which will trigger the function.
function mytheme_cron_function(){
//Do some things... your custom code goes here.
}
add_action('mytheme_cron_hook','mytheme_cron_function');//name of action, name of function to run
@MogulChris
MogulChris / functions.php
Created March 25, 2020 23:36
WordPress - disable warnings about a specific function
<?php
//Since 5.3 WordPress spits out this notice: add_submenu_page was called incorrectly. The seventh parameter passed to add_submenu_page() should be an integer representing menu position.
//I've fixed my own plugins, but it's still coming from a WP Engine mu-plugin which I can't fix or disable, and I still want WP_DEBUG_DISPLAY enabled so I can see other errors and warnings.
//Solution: apply_filters( 'doing_it_wrong_trigger_error', true, $function, $message, $version )
function mf_hide_annoying_notice($a, $function) {
if($function == 'add_submenu_page') return false;
}
add_filter('doing_it_wrong_trigger_error','mf_hide_annoying_notice',10,2);
@MogulChris
MogulChris / functions.php
Created June 15, 2020 23:15
Output HTML after WordPress menu items
<?php
//Use nav_menu_item_args hook to add extra HTML to a specific menu item. It will appear inside the <li>
//See $item and $args for other things you can inspect and modify
function mytheme_nav_menu_item_args($args,$item,$depth){
//print_r($item);
//print_r($args)
@MogulChris
MogulChris / functions.php
Created July 9, 2020 21:29
Custom WooCommerce product tabs from ACF repeater
<?php
//see https://gist.github.com/woogists/c15230393ba094d9da33a19a02e20a15 for the general approach to making custom tabs
add_filter( 'woocommerce_product_tabs', 'my_new_product_tabs' );
function my_new_product_tabs( $tabs ) {
$tabs = array(); //Remove default tabs (optional)
global $post;
@MogulChris
MogulChris / .htaccess
Last active August 18, 2020 05:00
Password protect a CodeIgniter subdirectory using .htpasswd, avoid 404 errors
#Using CPanel to password protect a CodeIgniter subdirectory, and getting a 404 error when trying to access the subdirectory? I got u fam.
# This goes in the .htaccess file in the directory you wish to protect
#The critical line is ErrorDocument 401 "Authorisation Required"
#----------------------------------------------------------------cp:ppd
# Section managed by cPanel: Password Protected Directories -cp:ppd
# - Do not edit this section of the htaccess file! -cp:ppd
#----------------------------------------------------------------cp:ppd
AuthType Basic
AuthName "My extremely secret directory"
@MogulChris
MogulChris / smoothscroll.js
Created August 19, 2020 02:53
Another smooth scroll approach
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
offset = 100; //px, enough to clear the sticky nav menu
$('html,body').animate({ scrollTop: target.offset().top - offset }, 600);
return false;
}
}
@MogulChris
MogulChris / functions.php
Created August 26, 2020 23:52
WP Bakery / Visual Composer custom css on WooCommerce shop page
<?php
// The issue:
// You want to use WP Bakery for your WooCommerce shop page.
// You can enable WP Bakery for that page, but custom Design Options are not showing.
// The explanation:
// WP Bakery keeps those custom design changes as a CSS string, stored as post meta on the page you are using for your shop page.
// Unfortunately that CSS is not being output by your theme, hence your custom design changes are not working.
@MogulChris
MogulChris / functions.php
Created September 1, 2020 04:49
Stop WooCommerce ruining your formatting with paragraph p tags wpautop
<?php
//Wonky formatting on WooCommerce pages and only on WooCommerce pages? P tags where you don't want P tags?
function remove_autop_for_wc(){
remove_filter('woocommerce_short_description','wpautop');
}
add_action( 'init', 'remove_autop_for_wc', 999999 );
?>