• Resolved ultravibe

    (@ultravibe)


    Hello,
    Trying to use the filter like so in WP:
    add_filter (‘apple_news_end_of_article_json’, array($this, ‘apple_news_end_of_article_filter’), 10, 2);

    Then returning JSON with a link to the article back on our site, but doesn’t seem to be actually firing or executing. Am I using it incorrectly?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Kevin Fodness

    (@kevinfodness)

    The end of article JSON is intended to be placed in the theme configuration. I think if the custom JSON for the end of article component is empty, the filter never fires. In the WordPress admin, you can go to Apple News > Customize JSON, then choose your theme, then select End of Article as the component, and save the JSON there.

    More info: https://github.com/alleyinteractive/apple-news/wiki/Customizing-JSON#end-of-article

    Thread Starter ultravibe

    (@ultravibe)

    I already have that – but the filter isn’t altering it in any way as would be expected from a WP filter. Trying to call the filter on publish, that would then add a link to the article back to our site…

    I thought this filter would alter the JSON output of the component and return it, so that one could for instance add some dynamically created content to the particular component – is that correct or am I misinterpreting its use? Thanks!

    Plugin Author Kevin Fodness

    (@kevinfodness)

    I think you are setting up the filter incorrectly, and that’s why it’s not getting called. That particular filter only accepts one parameter, but you’re indicating that it should accept two (the 10, 2 part).

    Following the structure of your example, I created a sample filter that did work:

    class AN_Test_Filter {
    	public function __construct() {
    		add_filter( 'apple_news_end_of_article_json', [ $this, 'apple_news_end_of_article_filter' ], 10, 1 );
    	}
    	public function apple_news_end_of_article_filter( $json ) {
    		return [
    			'role' => 'body',
    			'text' => 'test end of article',
    		];
    	}
    }
    
    $tt1_test_filter = new AN_Test_Filter();

    You could also do the same thing outside of a class context, like this:

    function my_theme_filter_apple_news_end_of_article_json( $json ) {
    	return [
    		'role' => 'body',
    		'text' => 'test end of article',
    	];
    }
    add_filter( 'apple_news_end_of_article_json', 'my_theme_filter_apple_news_end_of_article_json' );

    In these examples, I’m just returning a static body for the end of article content, but you can modify the value of the $json variable and return it as well, if you want to do things like replacing placeholder values or adding a link to the web version of the article etc.

    • This reply was modified 3 years, 1 month ago by Kevin Fodness. Reason: formatting
    • This reply was modified 3 years, 1 month ago by Kevin Fodness. Reason: formatting, again
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘apple_news_{$component_name}_json’ is closed to new replies.