Support » Plugin: Helpful » Control Helpful with ACF

  • Resolved heyjoecampbell

    (@heyjoecampbell)


    I would like to specify Helpful Before voting, After voting, and Answer buttons via Advanced Custom Fields (ACF) – on the post level.

    I am able to successfully change the Heading, Pro, and Contra outputs with the helpful.php file override.

    Sample Code
    <?php echo apply_filters( ‘helpful_headline_html’, ‘<div id=”wthf-title”>’ . get_field(‘poll_title’) . ‘</div>’ ); ?>

    Can I control the “After Voting” responses via ACF?

    If so, please provide instructions/documentation – Thank you πŸ˜€

    helpful_exists
    helpful_after_pro
    helpful_after_contra
    helpful_after_fallback

Viewing 15 replies - 1 through 15 (of 24 total)
  • Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    I would not use Helpful functions for this, but those from WordPress. So you can override options:

    
    /**
     * ACF Field: helpful_exists
     */
    add_filter( "pre_option_helpful_exists", 'pre_option_helpful_exists', 10, 2 );
    function pre_option_helpful_exists( $option, $default ) {
    	return get_field( 'helpful_exists', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_pro
     */
    add_filter( "pre_option_helpful_after_pro", 'pre_option_helpful_after_pro', 10, 2 );
    function pre_option_helpful_exists( $option, $default ) {
    	return get_field( 'helpful_after_pro', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_contra
     */
    add_filter( "pre_option_helpful_after_contra", 'pre_option_helpful_after_contra', 10, 2 );
    function pre_option_helpful_exists( $option, $default ) {
    	return get_field( 'helpful_after_contra', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_fallback
     */
    add_filter( "pre_option_helpful_after_fallback", 'pre_option_helpful_after_fallback', 10, 2 );
    function pre_option_helpful_exists( $option, $default ) {
    	return get_field( 'helpful_after_fallback', 'options' );
    }

    Source: https://developer.wordpress.org/reference/hooks/pre_option_option/

    • This reply was modified 6 months, 3 weeks ago by Pixelbart.
    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Hallo πŸ˜€

    The filters results in empty/blank responses for both Pro and Contra responses.

    Note: the code you provided had duplicate function names (from copying & pasting πŸ˜‰) I changed it before adding.

    
    /**
     * ACF Field: helpful_exists
     */
    add_filter( "pre_option_helpful_exists", 'pre_option_helpful_exists', 10, 2 );
    function pre_option_helpful_exists( $option, $default ) {
    	return get_field( 'helpful_exists', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_pro
     */
    add_filter( "pre_option_helpful_after_pro", 'pre_option_helpful_after_pro', 10, 2 );
    function pre_option_helpful_after_pro( $option, $default ) {
    	return get_field( 'helpful_after_pro', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_contra
     */
    add_filter( "pre_option_helpful_after_contra", 'pre_option_helpful_after_contra', 10, 2 );
    function pre_option_helpful_after_contra( $option, $default ) {
    	return get_field( 'helpful_after_contra', 'options' );
    }
    
    /**
     * ACF Field: helpful_after_fallback
     */
    add_filter( "pre_option_helpful_after_fallback", 'pre_option_helpful_after_fallback', 10, 2 );
    function pre_option_helpful_after_fallback( $option, $default ) {
    	return get_field( 'helpful_after_fallback', 'options' );
    }
    
    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    Right, I did not pay attention to that. I support here during my working hours and try to answer as good and fast as possible.

    Did you exchange the get_field( 'helpful_after_pro', 'options' ); with your get_field from ACF?

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    I created the fields using the names you provided for testing purposes:

    helpful_exists
    helpful_after_pro
    helpful_after_contra
    helpful_after_fallback

    If you do not mind, could you please test on your end.

    When confirmed, this would make for a great entry in your documentation πŸ˜€

    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    I had already tested this. But I do not use ACF. If ACF does not work, you might have to put the functions in the acf/init hook to make the fields of ACF available.

    Related: https://www.advancedcustomfields.com/resources/acf-init/

    My test without ACF works fine:

    add_filter( 'pre_option_helpful_heading', function( $option, $default ) {
      return 'test';
    }, 10, 2);

    I don’t include things in the documentation that have to do with third parties. Simply because there are so many things that people use. Helpful works with the WordPress utilities, so you can manipulate things with the WordPress utilities, for example with the pre_option_{option_name} Hook.

    In the future I will add filters for the texts so that you can change things with them.

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Thanks for the response. I was able to recreate your filter for the title on my site. But when I change “pre_option_helpful_heading” to “pre_option_helpful_after_pro” is results in a blank response.

    Please confirm on your end:

    
    add_filter( 'pre_option_helpful_after_pro', function( $option, $default ) {
      return 'pixelbart: Yes';
    }, 10, 2);
    
    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    Also works for me:

    add_filter( 'pre_option_helpful_after_pro', function( $option, $default ) {
      return 'pixelbart: Yes';
    }, 10, 2 );
    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Getting closer to a solution. I removed all of the previous and used the following:

    
    add_filter( 'pre_option_helpful_after_fallback', function( $option, $default ) {
      return 'pixelbart: Fallback';
    }, 10, 2 );
    

    It works, so now I know it has something to do with the “helpful_after_pro” and
    helpful_after_contra” – any idea what could be the issue?

    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    Helpful uses helpful_feedback_message_voted only if the vote was not recognized. So if sessions and cookies do not work.

    Then the vote is set to none and Helpful uses helpful_feedback_message_voted to show something anyway. This is a fallback for such cases.

    Maybe your server does not support sessions or you have disabled cookies in your browser.

    Just in case there is a plugin that can help you, because it makes the sessions available even on servers that don’t support them. It also helps with bugs with the WordPress API, because faulty sessions don’t send headers in advance anymore.

    Plugin: https://wordpress.org/plugins/wp-native-php-sessions/

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Thank you for clarifying.

    I had “Disable cookies and sessions” active for testing purposes along with “Enable to allow users to vote more than once in individual posts”.

    I now only have “Enable to allow users to vote more than once in individual posts” enabled and I manually reset results after every test interaction.

    I also realized the After Vote Text filter does not work on a per post basis. I wanted to customize the question, pro & contra, along with the each response option on a per post basis via custom fields. Now that I know this is not possible, I will simply focus on what I can control (question and answers).

    I greatly appreciate you assistance πŸ˜€.

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    In appreciation I sent you a cup of digital coffee πŸ˜€β˜•
    https://www.buymeacoffee.com/pixelbart/c/423303

    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    Without having tested it, you could try this with the global $post variable. Then you can use ACF to place fields inside the posts and control them.

    But I don’t know if the global variable is also available in the pre_option_{option_name} filter. If it is, this should work.

    But I will put it on the todo and build filters for it. So that this is easier. Also on post base.

    add_filter( 'pre_option_helpful_after_pro', function( $option, $default ) {
    	global $post;
    
    	if ( isset( $post->ID ) ) {
    		/**
    		 * Return acf field on post base
    		 */
    		return get_field( 'helpful_after_pro', $post->ID );
    	} 
    
    	return $option;
    
    }, 10, 2 );

    Thank you! But you really don’t have to do this. I only put this in because I was asked if and how you can leave a tip somewhere πŸ˜€

    This should not be an incentive to donate anything.

    Stay healthy and many thanks!

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    I believe people should donate to EVERY plugin developer (with a financial contribution, 5-STAR review, and/or sharing via social media). Showing appreciation is the right thing to do.

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    I tried the code – it did not work. I read somewhere that the functions.php file is outside of the loop and can not get the post ID.

    I am not a developer, so this is above me.

    Plugin Author Pixelbart

    (@pixelbart)

    @heyjoecampbell

    That you can not access the post when you are in functions.php is unfortunately not correct. The functions.php is always called. Therefore there should be no echo there.

    But with the update 4.4.10 there are new filters for you. I tested the filters and they worked for me. Also in the functions.php.

    add_filter( 'helpful_pre_after_pro', function( $content, $post_id ) {
    
    	if ( get_field( 'helpful_after_pro', $post_id ) ) {
    		return get_field( 'helpful_after_pro', $post_id );
    	}
    
    	return $content;
    }, 10, 2 );

    More filters: https://helpful-plugin.info/documentation/helpful_pre_name/

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Control Helpful with ACF’ is closed to new replies.