Created
November 18, 2019 02:07
-
-
Save redsoxfan2499/c755ebe6b65a547cdb1b7b662beca78b to your computer and use it in GitHub Desktop.
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
WP FILTERS/HOOK/ACTIONS | |
// WP Filters | |
function hwy_change_my_array( $value ) { | |
$value['fourth'] = 4; | |
return $value; | |
} | |
add_filter( 'hwy_my_array', 'hwy_change_my_array' ); | |
function hwy_change_my_array_again( $value ) { | |
$value['second'] = 222; | |
return $value; | |
} | |
add_filter( 'hwy_my_array', 'hwy_change_my_array_again', 5 ); | |
$my_array = apply_filters( 'hwy_my_array', array( 'first' => 1, 'second' => 2, 'three' => 3 ) ); | |
// WP Actions | |
function hwy_add_addiontal_content() { | |
echo "Add Addiontal Content"; | |
} | |
add_action('hwy_template_end', 'hwy_add_addiontal_content'); | |
?> | |
<div id="template"> | |
<h2>Heading</h2> | |
<p>Content</p> | |
<?php do_action( 'hwy_template_end' ); ?> | |
</div> | |
<?php | |
var_dump($my_array); | |
die(); | |
// action filters withing a PHP Class code example | |
class HWY_Plugin() { | |
function hwy_change_address( $address, $venue_id, $includeVenueName ){ | |
return '<span class="hwy_address">' . $address . '</span>'; | |
} | |
function hwy_add_venue_disclaimer() { | |
?> | |
<div class="disclaimer"> | |
NOTE: This venue is subject to change. | |
</div> | |
<?php | |
} | |
} | |
$my_plugin = new HWY_Plugin(); | |
add_filter( ' tribe_get_full_address', array( $my_plugin, 'hwy_change_address' ), 10, 3); | |
add_action( 'tribe_events_single_meta_venue_section_end', array( $my_plugin, 'hwy_add_venue_disclaimer')); | |
SHORTCODES | |
function hwy_handle_test_shortcode( $atts ) { | |
//return print_r( $atts, true ); | |
$atts = shortcode_atts( array( | |
'title' => 'Default Title', | |
), $atts); | |
return '<div class="test"><h2>' . $atts['title'] . '</h2> Test</div>'; | |
} | |
add_shortcode('my-test-shortcode', 'hwy_handle_test_shortcode'); | |
// use content between start and end shortcode tags and use PHP output buffer | |
function hwy_handle_my_shortcode( $atts, $content = '' ) { | |
$atts = shortcode_atts( array( | |
'color' => '#efefef', | |
), $atts); | |
ob_start(); | |
?> | |
<div class="test"> | |
<h2><?php echo $content; ?></h2> | |
<span style="color:<?php echo $atts['color'] ?>">Test</span> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
add_shortcode('my-shortcode', 'hwy_handle_my_shortcode'); | |
DISABLE GUTENBERG FOR ALL WP | |
/** | |
* Disable Gutenberg Block Editor | |
*/ | |
add_filter('use_block_editor_for_post', '__return_false'); | |
define( 'DISALLOW_FILE_EDIT', true ); | |
// create a Gutenberg block | |
npx create-guten-block my-block | |
cd my-block | |
npm start | |
// check if Gutenberg is enabled | |
if(defined('Gutenberg')) { } | |
// WP-CLI Commands | |
// create a block inside the plugin | |
wp scaffold block movie --title="My movie block" --plugin=movies | |
// create new plugin and new block | |
# Create plugin called books | |
$ wp scaffold plugin books | |
# Add a block called book to plugin books | |
$ wp scaffold block book --title="Book" --plugin=books | |
# Add a second block to plugin called books. | |
$ wp scaffold block books --title="Book List" --plugin=books | |
// update all plugins | |
wp plugin update --all | |
# Generate theme based on _s | |
$ wp scaffold _s sample-theme --theme_name="Sample Theme" --author="John Doe" | |
# Generate code for post type registration in given theme | |
$ wp scaffold post-type movie --label=Movie --theme=simple-life | |
LINUX command to tell file encoding cd into the directory where file located | |
file -I rua-blog-subscriber-lite.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment