• Resolved professor99

    (@professor99)


    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 Professor

    https://www.ads-software.com/extend/plugins/wp-user-frontend/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Tareq Hasan

    (@tareq1988)

    All the actions and filters I’ve provided by default came to the scene just like you did, necessity. The plugin is open source and if you think you can contribute like this one, please fork the repo and give me pull request in github, I’ll be happy to accept pull request ??

    Thread Starter professor99

    (@professor99)

    Will do Tareq.

    Will be updating the Edit Posts functionality during the next two weeks along the same lines as I have mentioned in my other posts. So once completed and tested I will fork it in GitHub.

    However will provide a link before that to the zipped code for the New Post functionality I’ve already implemented and tested including examples of it’s use.

    I will try contacting you direct via you Tareqs Planet contact page so I can give you a login to my test site so you can check out the work in action.

    I’m very happy to contribute to your most excellent plugin.

    Big Thanks

    TheProfessor

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Allow to Choose Category Filter’ is closed to new replies.