Question

I moved my site from http to https, but I wish to preserve my facebook likes. From what I read, I should use the og:url metatag so that facebook will know how to fix the likes on that page. I tried the following code, but it fails to change my urls from https to http in jetpack. Any suggestions?

function https_to_http_url( $url ) {
    $url = str_replace('https://', 'http://', $url );
    return $url;
}


function jetpack_og_url_https_to_http( $tags ) {
    // unset( $tags['og:url'] );

    $tags['og:url'] = https_to_http_url($tags['og:url']);
    return $tags;
}
add_filter( 'jetpack_open_graph_tags', 'jetpack_og_url_https_to_http' );
Was it helpful?

Solution

The code you used should work.

BTW to me, as the code is not working, the best approach would be to unset the og:url from Jetpack and do it on your own? I'm taking your code and adding mine and editing where necessary:

<?php
/**
 * Change HTTPS to HTTP
 * @param  string $url Default URL.
 * @return string      Modified URL.
 * -----------------------------------
 */
function https_to_http_url( $url ) {
    $url = str_replace('https://', 'http://', $url );
    return $url;
}


/**
 * Altering Jetpack OG URL
 * We're going to strip out the og:url produced by Jetpack.
 * @param  array $tags  Array of Open graph tags.
 * @return array        Modified array of Open graph tags.
 * -----------------------------------
 */
function jetpack_removing_og_url( $tags ) {
    //unset completely, we'll produce our own
    unset( $tags['og:url'] );

    return $tags;
}
add_filter( 'jetpack_open_graph_tags', 'jetpack_removing_og_url' );

/**
 * Add custom og:url
 * Adding our custom Open Graph URL meta tag on our own.
 * -----------------------------------
 */
function wpse233574_custom_og_url() {

    //Jetpack-way to retrieve the URL
    if ( is_home() || is_front_page() ) {
        $front_page_id = get_option( 'page_for_posts' );
        if ( 'page' == get_option( 'show_on_front' ) && $front_page_id && is_home() )
            $url = get_permalink( $front_page_id );
        else
            $url = home_url( '/' );
    } else if ( is_author() ) {
        $author = get_queried_object();
        if ( ! empty( $author->user_url ) ) {
            $url = $author->user_url;
        } else {
            $url = get_author_posts_url( $author->ID );
        }
    } else if ( is_singular() ) {
        global $post;
        $url = get_permalink( $post->ID );
    }

    //Modifying the URL for our custom purpose
    $modified_url = https_to_http_url( $url );

    //Finally print the meta tag
    echo '<meta property="og:url" content="'. esc_url($modified_url) .'" />';
}
add_action( 'wp_head', 'wpse233574_custom_og_url' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top