Is_single in custom-functions file not working

If I just put echo “hello”; that works.

But I have:

add_filter( 'wpseo_opengraph_url', 'my_opengraph_url' );
	function my_opengraph_url( $url ) {
		if(is_single() || is_singular()) {
			$checksingleid = get_the_ID();
			echo "IDHEREPost: $checksingleid";
			if($checksingleid < 7294) {
        		return str_replace( 'https://', 'http://', $url );
        	}
        }
	}

I’m having two issues. One, is_single or is_singular is not working. I thought when opening a post page it would echo what I want. It’s not doing that.

Second, it’s not giving me a post ID out of the custom-functions file and can’t figure out how to get that.

All feedback appreciated.

Cheers!
Ryan

Try this:

add_filter( 'wpseo_opengraph_url', 'my_opengraph_url' );
	
function my_opengraph_url( $url ) {
        global $post; 

	if(is_single() || is_singular()) {
		$checksingleid = $post->ID;
		echo "IDHEREPost: $checksingleid";
		if($checksingleid < 7294) {
        		return str_replace( 'https://', 'http://', $url );
        	}
        }
}

Thanks, global post was definitely the magic. Here was my final solution:

add_filter( 'wpseo_opengraph_url', 'my_opengraph_url' );
	function my_opengraph_url( $url ) {
		global $post;
		if( 'post' == get_post_type( $post ) ) {
			$checksingleid = get_the_ID($post);
			if($checksingleid < 7294) {
        		return str_replace( 'https://', 'http://', $url );
        	}
        }

Cheers!
Ryan

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.