-2

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' );
  • Can someone please explain why this is getting down votes? This has to do with using filters in wordpress, what am I missing? – Tal Galili Jul 31 '16 at 16:13
  • 3
    Because the solution requires an external plugin outside of WP Core. This site isn't for plugin specific questions which could be addressed on the author's support pages. You'll see a lot of down votes for WooCommerce as well. – jgraup Jul 31 '16 at 16:40
  • I see. But it deals with jetpack which is a basic plugin from automatic. OH well, I'm now sad I place a bounty on this question. – Tal Galili Jul 31 '16 at 18:20
  • 1
    I see you have tried to unset the og:url key. Has that worked? Have you tried dumping the $tags array with var_dump() after running your search and replace? What's the output? – kovshenin Aug 2 '16 at 10:35
  • 1
    Could you elaborate on "it fails to change my urls from https to http in Jetpack" - do you mean that the produced 'og:url' meta-tag still starts with https://, or something else? – bosco Aug 2 '16 at 19:02
2
+50

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.