admin管理员组文章数量:1022964
I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here:
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here: https://codeshare.io/50MKJg
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
Share Improve this question edited Apr 17, 2019 at 11:27 staffsdesigner asked Apr 17, 2019 at 9:46 staffsdesignerstaffsdesigner 34 bronze badges 4 |1 Answer
Reset to default 0You're on the right path with your add_filter
, but this is the right way to implement filters.
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
return array( 'productions' );
}
The hook add_filter
must call a function that returns something to pass to the apply_filters
in the code you referenced.
You should add in whatever logic you need to return the correct post types and handle the default as well. For example:
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
// Assuming you set an option to control which post types should be used.
$custom_gig_post_types = get_option( 'gig_post_types', false );
if ( $custom_gig_post_types ) {
return $custom_gig_post_types;
}
return $post_types;
}
I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here:
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here: https://codeshare.io/50MKJg
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
Share Improve this question edited Apr 17, 2019 at 11:27 staffsdesigner asked Apr 17, 2019 at 9:46 staffsdesignerstaffsdesigner 34 bronze badges 4-
1
Undefined variables in your code:
$wpdb
and$show_related
. Consider getting a proper IDE like PHPStorm. It has WordPress support and coding standards built-in. – norman.lol Commented Apr 17, 2019 at 10:01 - Thanks @leymannx — that's the plugin itself, and only a sample, relating to teh post types... The plugin author suggested only the hook "gigpress_related_post_types" was needed to target it in functions, but can't offer support or details. – staffsdesigner Commented Apr 17, 2019 at 10:38
- What’s your code that you’re using to try and change the value. – Jacob Peattie Commented Apr 17, 2019 at 10:56
-
Thanks @JacobPeattie... I've tried a million variations... [well, ten+?) Things along the lines of, have been suggested to me:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
– staffsdesigner Commented Apr 17, 2019 at 11:07
1 Answer
Reset to default 0You're on the right path with your add_filter
, but this is the right way to implement filters.
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
return array( 'productions' );
}
The hook add_filter
must call a function that returns something to pass to the apply_filters
in the code you referenced.
You should add in whatever logic you need to return the correct post types and handle the default as well. For example:
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
// Assuming you set an option to control which post types should be used.
$custom_gig_post_types = get_option( 'gig_post_types', false );
if ( $custom_gig_post_types ) {
return $custom_gig_post_types;
}
return $post_types;
}
本文标签: Calling hooks in functions
版权声明:本文标题:Calling hooks in functions 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579538a2157227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$wpdb
and$show_related
. Consider getting a proper IDE like PHPStorm. It has WordPress support and coding standards built-in. – norman.lol Commented Apr 17, 2019 at 10:01add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
– staffsdesigner Commented Apr 17, 2019 at 11:07