Forum Replies Created

Viewing 9 replies - 1 through 9 (of 9 total)
  • OK, how about going the other way, changing the content to match the rules that work?

    Before calling the transformer, the plugin has this.
    $content = apply_filters( ‘the_content’, $content );

    So in functions.php, you should be able to add something like this:

    add_filter( 'instant_articles_content', 'change_ia_content', 10, 3 );
    
    function change_ia_content ( $content ) {
    	$pattern = '~<p><span class='embed-youtube' style='text-align:center; display: block;'><iframe.*</iframe></span></p>~';
    	preg_match_all($pattern, $content, $matches);
    
    	foreach ($matches[0] as $match) {
    		$wrappedframe = str_replace("<p><span class='embed-youtube' style='text-align:center; display: block;'>", "", $match);
    		$wrappedframe = str_replace('</span></p>', '', $wrappedframe);
    		//replace original iframe with new in content
    		$content = str_replace($match, $wrappedframe, $content);
    	}
        return $content;
    }

    I don’t know if that code is exactly right, but I think that idea should work.

    Then you should be able to remove the custom rules and you should be in the same position as agk4444, for whom everything works.

    Last thought. I wonder if class=”youtube-player” is breaking this. It shouldn’t, but, who knows? So you could similarly replace that out in the function above.

    Good luck!

    Yes, the span will prevent this from working. The selector “selector” : “//p[iframe]” won’t catch it as the iframe isn’t directly in the paragraph.

    I think you just need to replace the selector in the rule above.

    I’m not great with XPath. I think this might work:
    “selector” : “//iframe[@class=’youtube-player’]”

    They have good hooks that let you overwrite these. I added the following to my functions.php to use the second category name as the kicker if the first category name is Instant Articles.

    function change_ia_kicker( $category_name, $post_ID ) {
    //Next block is my specific code
    	if ('Instant Articles' == $category_name) {
    		$categories = get_the_category($post_ID);
    
    		if ( is_array( $categories ) && count( $categories > 1) && isset( $categories[1]->name ) && __( 'Uncategorized' ) !== $categories[1]->name ) {
    			$category_name = $categories[1]->name;
    		}
    	}
    //At the end, return the new kicker
        return $category_name;
    }
    add_filter( 'instant_articles_cover_kicker', 'change_ia_kicker', 10, 3 );

    I had a similar problem with Vimeo videos. I wrote up my solution here: Errors Publishing Facebook Instant Articles with Video. Let me know if that solves it for you.

    Regarding AdSense, I don’t think that’s supported. We’re just using Facebook’s ads.

    Good luck!

    What iframe video are you using? Can you paste the code and any error or warning that you’re getting?

    Try setting this as the Custom Rules in the Instant Articles settings:

    {
        "rules":
        [{
            "class": "SocialEmbedRule",
            "selector" : "//p[iframe]",
            "properties" : {
                "socialembed.url" : {
                    "type" : "string",
                    "selector" : "iframe",
                    "attribute": "src"
                },
                "socialembed.height" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "height"
                },
                "socialembed.width" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "width"
                },
                "socialembed.iframe" : {
                    "type" : "children",
                    "selector" : "iframe"
                },
                "socialembed.caption" : {
                    "type" : "element",
                    "selector" : "figcaption"
                }
            }
    	}]
    }

    I set this as the Custom Rules in the Settings, and it solved a very similar problem for me. It takes a rule that’s in the rules-configuration.json file, but I added the height and width parts.

    {
        "rules":
        [{
            "class": "SocialEmbedRule",
            "selector" : "//p[iframe]",
            "properties" : {
                "socialembed.url" : {
                    "type" : "string",
                    "selector" : "iframe",
                    "attribute": "src"
                },
                "socialembed.height" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "height"
                },
                "socialembed.width" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "width"
                },
                "socialembed.iframe" : {
                    "type" : "children",
                    "selector" : "iframe"
                },
                "socialembed.caption" : {
                    "type" : "element",
                    "selector" : "figcaption"
                }
            }
    	}]
    }

    I set the custom rules as below, to get the plugin to write out the height and width that are in the original source. There are a few different SocialEmbed selectors, so if this doesn’t work on your code you may want to add height and width to other SocialEmbedRules.

    {
        "rules":
        [{
            "class": "SocialEmbedRule",
            "selector" : "//p[iframe]",
            "properties" : {
                "socialembed.url" : {
                    "type" : "string",
                    "selector" : "iframe",
                    "attribute": "src"
                },
                "socialembed.height" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "height"
                },
                "socialembed.width" : {
                    "type" : "int",
                    "selector" : "iframe",
                    "attribute": "width"
                },
                "socialembed.iframe" : {
                    "type" : "children",
                    "selector" : "iframe"
                },
                "socialembed.caption" : {
                    "type" : "element",
                    "selector" : "figcaption"
                }
            }
    	}]
    }

    phpcodechecker.com recommends replacing isset(INSTANT_ARTICLES_SLUG) with null!==INSTANT_ARTICLES_SLUG. I made that change on very similar code, and it worked.

Viewing 9 replies - 1 through 9 (of 9 total)