Examples used at http://msls.co/hooks-filters-and-actions/
Last active
July 3, 2019 04:43
-
-
Save lloc/5f922fc770d818365992 to your computer and use it in GitHub Desktop.
Hooks, filters and actions
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
<?php | |
/** | |
* @param string $path | |
* @return string | |
*/ | |
function my_msls_admin_icon_get_edit_new( $path ) { | |
return add_query_arg( array( 'abc' => 'xyz' ), $path ); | |
} | |
add_filter( 'msls_admin_icon_get_edit_new', 'my_msls_admin_icon_get_edit_new' ); |
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
<?php | |
/** | |
* @param string $page | |
* @param string $section | |
*/ | |
function my_msls_admin_main_section( $page, $section ) { | |
add_settings_field( 'custom_field', __( 'Custom Field' ), 'custom_field', $page, $section ); | |
} | |
add_action( 'msls_admin_main_section', 'my_msls_admin_main_section', 10, 2 ); | |
function custom_field() { | |
printf( | |
'<input id="custom_field" name="msls[custom_field]" value="%s" size="30"/>', | |
esc_attr( MslsOptions::instance()->custom_field ) | |
); | |
} |
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
<?php | |
/** | |
* @param string $page | |
*/ | |
function my_msls_admin_register( $page ) { | |
add_settings_section( 'custom_section', __( 'Custom Settings' ), 'custom_section', $page ); | |
add_settings_field( 'custom_field', __( 'Custom Field' ), 'custom_field', $page, 'custom_section' ); | |
} | |
add_action( 'msls_admin_register', 'my_msls_admin_register' ); | |
function custom_section() { } | |
function custom_field() { | |
printf( | |
'<input id="custom_field" name="msls[custom_field]" value="%s" size="30"/>', | |
esc_attr( MslsOptions::instance()->custom_field ) | |
); | |
} |
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
<?php | |
/** | |
* @param array $arr | |
* @return array | |
*/ | |
function my_msls_admin_validate( $arr ) { | |
$arr['abc'] = ( isset( $arr['abc'] ) ? $arr['abc'] : 'xyz' ); | |
} | |
add_filter( 'msls_admin_validate', 'my_msls_admin_validate' ); |
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
<?php | |
/** | |
* @param array $blogs | |
* @return array | |
*/ | |
function my_msls_blog_collection_construct( $blogs ) { | |
if ( isset( $blogs[1] ) ) { | |
unset( $blogs[1] ); | |
} | |
return $blogs; | |
} | |
add_filter( 'msls_blog_collection_construct', 'my_msls_blog_collection_construct' ); |
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
<?php | |
/** | |
* @param int $blog_id | |
* @param string|bool $description | |
* @return string|bool | |
*/ | |
function my_msls_blog_collection_description( $blog_id, $description = false ) { | |
$arr = array( 1 => 'abc', 2 => 'xyz' ); | |
if ( isset( $arr[ $blog_id ] ) ) { | |
$description = $arr[ $blog_id ]; | |
} | |
return $description; | |
} | |
add_filter( 'msls_blog_collection_description', 'my_msls_blog_collection_description', 10, 2 ); | |
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
<?php | |
/** | |
* @param string $output | |
* @param array $links | |
* @return string | |
*/ | |
function my_msls_filter_string( $output, $links ) { | |
if ( empty( $links ) ) { | |
$output = ''; | |
} | |
else { | |
$output = sprintf( | |
'<p>%s</p><ul><li>%s</li></ul>', | |
__( 'Available Translations' ), | |
implode( '</li><li>', $links ) | |
); | |
} | |
return $output; | |
} | |
add_filter( 'msls_filter_string', 'my_msls_filter_string', 10, 2 ); |
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
<?php | |
/** | |
* @param string $language | |
* @return string | |
*/ | |
function my_msls_head_hreflang( $language ) { | |
if ( 'en' == $language ) { | |
$language = 'en-gb'; | |
} | |
return $language; | |
} | |
add_filter( 'msls_head_hreflang', 'my_msls_head_hreflang' ); |
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
<?php | |
/** | |
* @param int $display | |
* @return MslsLink | |
*/ | |
function my_msls_link_create( $display ) { | |
class MyMslsLink extends MslsLink { | |
protected $format_string = '<img src="{src}" alt="{alt}"/> <span>{txt}</span>'; | |
} | |
return new MyMslsLink; | |
} | |
add_filter( 'msls_link_create', 'my_msls_link_create' ); |
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
<?php | |
/** | |
* @param int $object_id | |
* @param string $class | |
*/ | |
function my_msls_main_save( $object_id, $class ) { | |
// Your code here | |
} | |
add_action( 'msls_main_save', 'my_msls_main_save' ); |
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
<?php | |
/** | |
* @param array $args | |
* @return array | |
*/ | |
function my_msls_meta_box_render_select_hierarchical( $args ) { | |
$args['post_status'] = array( 'publish', 'draft', 'future', 'pending' ); | |
return $args; | |
} | |
add_filter( 'msls_meta_box_render_select_hierarchical', 'my_msls_meta_box_render_select_hierarchical' ); |
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
<?php | |
/** | |
* @param array $args | |
* @return array | |
*/ | |
function my_msls_meta_box_suggest_args( $args ) { | |
$args['posts_per_page'] = 5; | |
return $args; | |
} | |
add_filter( 'msls_meta_box_suggest_args', 'my_msls_meta_box_suggest_args' ); |
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
<?php | |
/** | |
* @param WP_Post $post | |
* @return WP_Post | |
*/ | |
function my_msls_meta_box_suggest_post( $post ) { | |
$post->post_title .= ' (' . $post->ID . ')'; | |
return $post; | |
} | |
add_filter( 'msls_meta_box_suggest_post', 'my_msls_meta_box_suggest_post' ); |
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
<?php | |
/** | |
* @param array $languages | |
* @return string | |
*/ | |
function my_msls_options_get_available_languages( languages ) { | |
if ( ! isset( $languages['en_GB'] ) ) { | |
$languages['en_GB'] = 'British English'; | |
} | |
return $languages; | |
} | |
add_filter( 'msls_options_get_available_languages', 'my_msls_options_get_available_languages' ); |
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
<?php | |
/** | |
* @param string $url | |
* @return string | |
*/ | |
function my_msls_options_get_flag_url( $url ) { | |
return get_stylesheet_directory_uri() . '/images/'; | |
} | |
add_filter( 'msls_options_get_flag_url', 'my_msls_options_get_flag_url' ); |
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
<?php | |
/** | |
* @param string $postlink | |
* @param string $language | |
* @return string | |
*/ | |
function my_msls_options_get_permalink( $url, $language ) { | |
if ( 'de_DE' == $language ) { | |
$url = str_replace( '/products/', '/produkte/', $url ); | |
} | |
return $url; | |
} | |
add_filter( 'msls_options_get_permalink', 'my_msls_options_get_permalink', 10, 2 ); |
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
<?php | |
/** | |
* @param string $url | |
* @param MslsLink $link | |
* @param bool current | |
* @return string | |
*/ | |
function my_msls_output_get( $url, $link, $current ) { | |
return sprintf( | |
'<a href="%s" title="%s"%s>%s</a>', | |
$url, | |
$link->txt, | |
( $current ? ' class="current"' : '' ), | |
$link | |
); | |
} | |
add_filter( 'msls_output_get', 'my_msls_output_get', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment