Skip to content

Instantly share code, notes, and snippets.

View MrJoshFisher's full-sized avatar

Josh Fisher MrJoshFisher

View GitHub Profile
@MrJoshFisher
MrJoshFisher / functions.php
Last active August 3, 2021 16:12
[WordPress Ajax Form Submission] #WordPress
function add_theme_scripts()
{
wp_enqueue_style('style', get_stylesheet_uri(), array(), time());
wp_enqueue_style('jquery-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
wp_enqueue_script('scripts', get_theme_file_uri() . '/scripts.js', array( 'jquery-ui-tabs' ), time(), true);
wp_localize_script('scripts', 'settings', array(
'ajaxurl' => admin_url('admin-ajax.php'),
));
}
@MrJoshFisher
MrJoshFisher / single.php
Last active December 13, 2020 00:05
[Track WordPress Post Views by Using Post Meta] #WordPress
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
@MrJoshFisher
MrJoshFisher / functions.php
Created December 13, 2020 00:07
[Add an Admin User with PHP] #WordPress
<?php
//Create an admin user
function smartwp_create_admin_user(){
$username = 'yourusername';
$password = '2JyAEQJ9B9Jf5T8a';
$email = '[email protected]';
//This will ensure it only tries to create the user once (based on email/username)
if ( !username_exists( $username ) && !email_exists( $email ) ) {
$userid = wp_create_user( $username, $password, $email );
$user = new WP_User( $userid );
@MrJoshFisher
MrJoshFisher / functions.php
Created February 3, 2021 09:43
[Popular Posts]
function wpb_set_post_views($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
@MrJoshFisher
MrJoshFisher / functions.php
Created March 15, 2021 15:31
[Disable Emojis]
/**
* Disable the emoji's
*/
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
function wpb_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'wpb' ),
'id' => 'sidebar-1',
'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
@MrJoshFisher
MrJoshFisher / scripts.js
Created May 17, 2021 09:46
[get URL parameter using jQuery or plain JavaScript]
/* https://stackoverflow.com/questions/19491336/how-to-get-url-parameter-using-jquery-or-plain-javascript */
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
@MrJoshFisher
MrJoshFisher / .htaccess
Created May 17, 2021 15:02
[Add .html to URLS]
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.[a-zA-Z0-9]{3,4}
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.html
@MrJoshFisher
MrJoshFisher / functions.php
Created May 25, 2021 14:04
[Get Featured Image]
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
@MrJoshFisher
MrJoshFisher / functions.php
Created July 5, 2021 16:30
[Chnage return to shop url] #wordpress
function store_mall_wc_empty_cart_redirect_url() {
$url = 'http://example.com/sample-page'; // change this link to your need
return esc_url( $url );
}
add_filter( 'woocommerce_return_to_shop_redirect', 'store_mall_wc_empty_cart_redirect_url' );