Allow to Choose Category Filter
-
Normally there are a squillion requests for special features to plugin authors. The best response is generally to tell the requester to write the code themselves or for them to ask guidance from the community to implement their special request.
However if the request is useful to many it’s maybe worth implementing.
I congratulate the author of this plugin “Tareq” for implementing this plugin with lots of actions and filters inbuilt which make it easy to adapt this plugin externally to many uses.
I preferably try to avoid modifying core plugin code as it prohibits upgrades so WordPress’s filters and actions avoid this if liberally used by the plugin author.
The following request is simply for the addition of such a filter.
Presently the WP User Frontend:Settings” – “Frontend Posting” -> “Allow to choose category?” option allows for the suppression of the “Category” field in the Add Post and Edit Post pages and the setting of the category defaulted to the accompanying field “Default post category”. In the code this option is called ‘allow_cats’.
However it is beneficial to allow this option to be set programmatically for various posts. This allows the category to be set programmatically as well. Whilst the setting of this may be done by programmatically by setting the option via the wpuf_options_frontend filter it is more specific and obvious to do it right on the code that uses it.
Now the code for this is quite simple. I’ve only implemented it in wpuf-add-post.php so far but it also needs to happen for the Edit Page code as well.
Basically replace code like the following
<?php if ( wpuf_get_option( 'allow_cats' ) == 'on' ) {
with
<?php $allow_cats = wpuf_get_option( 'allow_cats' ); $allow_cats = apply_filters( 'wpuf_allow_cats', $allow_cats ); if ( $allow_cats == 'on' ) { ?>
To illustrate it’s use the following code allows the setting of the category and a taxonomy externally on a new post for certain URLs.
URL: https://test.org.au/new-post?showtime_taxonomy=Showtime-120
showtime.php
============//Suppress category field for WP User Frontend for show blogs function showtime_wpuf_allow_cats($val) { //Used by WP User Frontend Add Post Editor both initial and post requests. $showtime_taxonomy = $_GET['showtime_taxonomy']; if(!isset($showtime_taxonomy)) return $val; //Is Showtime request. //Turn off WP User Frontend "allow_cats" option. return 'off'; } add_filter( 'wpuf_allow_cats', 'showtime_wpuf_allow_cats', 10, 1 ); //Set post category to Showtime (bypass default setting) function showtime_wpuf_add_post_args($post_args) { $showtime_taxonomy = $_GET['showtime_taxonomy']; if(!isset($showtime_taxonomy)) return $post_args; //Set category to Showtime. //Note we use a Showtime category to avoid all Shows blogs ending up in 'uncategorized' category. //NB Although it's possible to omit a category for an article the admin edit post inserts the //'uncategorized' category on update. $category = get_term_by('name','Showtime','category'); $category_id = $category->term_id; $post_args['post_category'] = array($category_id); //Set taxonomy $taxonomy = get_term_by('name',$showtime_taxonomy,'showtime'); $taxonomy_id = $taxonomy->term_id; $post_args['tax_input'] = array('showtime' => $taxonomy_id); return $post_args; } add_filter('wpuf_add_post_args', 'showtime_wpuf_add_post_args', 10, 1 );
Such code makes WP User Frontend a great tool for WordPress programmers.
Cheers
The Professorhttps://www.ads-software.com/extend/plugins/wp-user-frontend/
- The topic ‘Allow to Choose Category Filter’ is closed to new replies.