Skip to content

Instantly share code, notes, and snippets.

@NaszvadiG
Forked from josanua/wp-helper.php
Created January 25, 2024 23:54
Show Gist options
  • Save NaszvadiG/b6085ddc67757506cf4d7c835ddb50a6 to your computer and use it in GitHub Desktop.
Save NaszvadiG/b6085ddc67757506cf4d7c835ddb50a6 to your computer and use it in GitHub Desktop.
wordpress helper file
<?php
/************ Global Variables and settings ************/
// for external file to include WP environment
require( '../sitename-base/wp-load.php' );
// Global Var
// https://codex.wordpress.org/Global_Variables
//To access a global variable in your code, you first need to globalize the variable with
global $variable;
/************ Disable autoupdate ************/
https://codex.wordpress.org/Configuring_Automatic_Background_Updates
// quick overview
https://wordpress.stackexchange.com/questions/120081/how-do-i-configure-automatic-updates-in-wordpress-3-7
// add the following line to wp-config.php:
// for core only
define( 'AUTOMATIC_UPDATER_DISABLED', true ); // To completely disable all types of automatic updates
define( 'WP_AUTO_UPDATE_CORE', false );
define( 'WP_AUTO_UPDATE_CORE', minor ); //allow minor updates only
// Disable Plugin and Theme Update and Installation
define( 'DISALLOW_FILE_MODS', true );
// use functions.php for these snippets, deactivate update, deactivate theme update
add_filter( 'auto_update_theme', '__return_false' );
add_filter( 'auto_update_plugin', '__return_false' );
// disable update notifications
function remove_core_updates(){ global $wp_version;return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,); }
add_filter('pre_site_transient_update_core','remove_core_updates');
add_filter('pre_site_transient_update_plugins','remove_core_updates');
add_filter('pre_site_transient_update_themes','remove_core_updates');
// Via filter with MU plugins
add_filter( 'automatic_updater_disabled', '__return_true' );
/************ How to include files ************/
//for external file to include WP environment (tested) - require( '../wordpress/wp-load.php' );
include_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
//Another method
require_once(dirname(__FILE__) . '/options.php');
//WP API
get_template_part('template part name');//without .php extension
//General PHP Methods
include()
include_once()
require()
require_once()
// ------------- Work with Custom Fields Posts ------------- //
//Show post custom field
get_post_meta('id', 'custom filed name(meta key)', true or false (true default));
// Show post custom field values
$custom_fields = get_post_custom(372);
// $my_custom_field = $custom_fields['my_custom_field'];
foreach ( $custom_fields as $key => $value ) {
echo $key . " => " . $value . "<br/>" ;
}
// --> Returns an array containing the keys of all custom fields of a particular post or page. <-- //
$postID = 111;
$custom_field_keys = get_post_custom_keys($postID);
foreach ( $custom_field_keys as $key => $value ) {
$valuet = trim($value);
if ( '_' == $valuet{0} )
continue;
echo $key . " => " . $value . "<br />";
}
// Curata de semnul _
if ( '_' == $valuet{0} );
// Titlul postului
echo get_post(get_the_ID())->post_title;
// Cod utilizat
// echo get_post(372)->post_title . "<br/>";
// echo get_post_meta(372, 'wpcf-adresa', TRUE) . "<br/>";
// echo get_post_meta(372, 'wpcf-persoana-de-contact', TRUE) . "<br/>";
// echo get_post_meta(372, 'wpcf-mobil', TRUE) . "<br/>";
// echo get_post_meta(372, 'wpcf-email', TRUE) . "<br/>";
$postID = 111;
$custom_field_keys = get_post_custom_keys($postID);
foreach ( $custom_field_keys as $key => $value ) {
$valuet = trim($value);
if ( '_' == $valuet{0} )
continue;
echo get_post_meta($postID, $value , TRUE) . "<br/>";
}
/************ ShortCode ************/
// Create shortcode
add_shortcode( $tag , $func );//$tag - tag name to use in template, $func - function name to execute
add_shortcode( 'name' , $func );
//Read shortcode
do_shortcode();
do_shortcode('[shortcode-name]');//Example of Contact Form integration - <?php echo do_shortcode( '[contact-form-7 id="1135" title="Contact form 1"]' );
add_filter('the_content', 'do_shortcode', 11);
// To see Shortcode in widgets
add_filter('widget_text', 'do_shortcode');
// Testing
add_shortcode('test', 'show_text_test');
function show_text_test(){
return "Tesing shortcode text";
echo "Tesing shortcode text"; //will be another formated results
}
// Troubles with outpup before other content, poot this code in add_shortcode function
ob_start();
...your code...
return ob_get_clean();
// Little explanation
// php just outputs your content right away when its see print statement. What we do here is, we are holding all the output in buffer and not giving it in print until the whole things finish.
// then we are returning the final whole results(outputs). This gives control on our side to when and where to print outputs.
// Hide the broken Shortcodes
// This code adds back the orphan shortcode with no output. Don’t forget to replace shortcodetag with your shortcode name.
add_shortcode( 'shortcodetag', '__return_false' );
/************ Capabilities ************/
/* Roles with numbers is deprecated */
8 = manage_options;
/************ jQuery ************/
// include custom jQuery
// Sa nu uitam de ordinea de includere si executie a scripturilor si bibliotecilor
function shapeSpace_include_custom_jquery() {
//Exclude default WP jQuery library
wp_deregister_script('jquery');
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'shapeSpace_include_custom_jquery');
//jQuery No conflict mode script ( don't work :( )
var $j = jQuery.noConflict();
$j(function(){
$j("#").method(function(){
$j(this).stop().animate({ Code });
});
)}
// jQuery in Wordpress
// By default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).
// Your solution of wrapping it in function will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).
// If you must use document.ready, you can actually pass $ into the function call:
// jQuery(function ($) { ... } or this (function($){your code})(jQuery);
// Working method !!!
(function($) {
// $ Works! You can test it with next line if you like
console.log($);
})( jQuery );
// Write this if you need it in header
jQuery(document).ready(function( $ ) {
$ Works! You can test it with next line if you like
// console.log($);
});
/************ Mail ************/
add_action('phpmailer_init','send_smtp_email');
function send_smtp_email( $mail ) {
// Define that we are sending with SMTP
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
//$mail->Host = 'spamfilter.starnet.md';//'smtp.gmail.com';
$mail->Host = 'mail.wippodev.com';
$mail->Port = '587';//'465';
$mail->SMTPAuth = true;//true;
$mail->Username = "info@web.visit-moldova.md";
$mail->Password = "j10294j8912h89h7!@";
$mail->SMTPSecure = "tls";
$mail->IsHTML(true);
}
/************ Hooks, Filters and Actions ************/
//Filters
apply_filters($tag, $value);
apply_filters($tag, $value, $var1, $var2, ... );
add_filter($tag, $function_to_add, $priority, $accepted_args);
//Actions
do_action($tag, $arg);
do_action($tag, $arg a, $arg b, ... );
add_action($tag, $function_to_add, $priority, $accepted_args);
/************ taxonomies ************/
site https://codex.wordpress.org/Taxonomies
// Get terms
site https://codex.wordpress.org/Function_Reference/the_terms
the_terms( $post->ID, 'people', 'People: ', ', ', ' ' );
$terms = get_terms( array(
'taxonomy' => 'your_custom_taxonomy',
'hide_empty' => false,
));
foreach($terms as $term){ echo $term->name . '<br/>';}
or
foreach( $terms as $term ){
print_r($term);
}
// Querying by taxonomy
$query = new WP_Query( array( 'person' => 'bob' ) );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'person',
'field' => 'slug',
'terms' => 'bob'
)
)
);
$query = new WP_Query( $args );
/************ Trix mix ************/
site https://tutes.in/how-to-change-wordpress-user-roles-using-mysql-database/
//Change user role
in wp_capabilities under meta_key column.
Subscriber
a:1:{s:10:"subscriber";b:1;}
Contributor
a:1:{s:11:"contributor";b:1;}
Author
a:1:{s:6:"author";b:1;}
Editor
a:1:{s:6:"editor";b:1;}
Administrator
a:1:{s:13:"administrator";b:1;}
/************ How To Fix HTTP Error When ************/
https://www.hostinger.com/tutorials/http-error-when-uploading-images-to-wordpress
https://www.wpbeginner.com/wp-tutorials/how-to-fix-the-http-image-upload-error-in-wordpress/
// Increase WordPress Memory Limit
define( 'WP_MEMORY_LIMIT', '256M' );
/************ Cookies ************/
https://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment