Add Parameters to Wordpress URL

Asked

Viewed 793 times

1

I have a plugin that manages links in Wordpress, with images and texts pretending to be related articles, all are external links. (Taboola type).

I would like to add at the end of the Links some parameters. Ex:

Link ==> othersite.com.br

I want to add now the following:

Link ==> othosite.com.br? src=urldinamicadowordpress

So I can know the origin of these clicks and which article is converting the most clicks.

If possible, still add if the click came from mobile or Desktop

Link ==> othosite.com.br? src=urldinamicadowordpress|mobile

Link ==> othosite.com.br? src=urldinamicadowordpress|desktop

Code of Redirection

add_action('init', 'count_clicks');
function count_clicks()
    {
    global $wpdb;
    if(isset($_GET['action']) && $_GET['action'] == 'count') {
    $id = $_GET['id'];
    $tableName = $wpdb->prefix . 'post_related_meta';

    $meta = $wpdb->get_results($wpdb->prepare("select meta_clicks, meta_link from $tableName where meta_id = %s", $id));
    $meta = $meta[0];

    $wpdb->update($wpdb->prefix."post_related_meta", array(
        'meta_clicks' => (int)$meta->meta_clicks+1,
    ), array(
        'meta_id' => $_GET['id']
    ),
        array(
            '%d'
        )
    );

    $redirect = $meta->meta_link;
    header("Location: $redirect");
    exit;
}
}

1 answer

1


How to do this depends a little on how links to external articles are generated. If you have control of them, just put something like this:

$url_dinamica = $_REQUEST['REQUEST_URI'];
$query = http_build_query( array( 'src' => $url_dinamica ) );

and place at the end of the existing link:

<a href="http://example.com/?<?php echo esc_attr( $query ); ?>">Link</a>
// imprime http://example.com/?src=seusite.com.br/pagina/do/link

To distinguish between mobile and desktop you can use the native function wp_is_mobile()

$url_dinamica = $_SERVER['REQUEST_URI'];
$url_dinamica .= '|' . ( wp_is_mobile() ? 'mobile' : 'desktop' );

This solution only works if you don’t have a complete page cache. If you do, you’ll need a javascript solution.

EDIT

Updating according to the added code. To work just put the query at the end of $meta->meta_link;:

$redirect = rtrim( $meta->meta_link, '/') .'/?'. $query;
header("Location: $redirect");
exit();

The rtrim() there is to ensure that we will not have duplicated bars at the end of the URL. I am considering that $meta->meta_link never has parameters, it is always a simple URL. If you have to change this code to test by parameters and use ? or & adequately.

  • Got it, I took a test here and it really works! Thank you very much! But in my plugin I don’t think it can be like this, for the simple reason of having a redirect. Unless you want to pass these parameters between urls. URL that counts Click and Redirect: http://meusite.com.br/? action=count&id=1&src=testeparam Directs to: meusite.com.br I wanted you to stay: meusite.com.br? src=testeparam taking this from the first url that counts clicks.

  • can do if you can intercept this redirect. Put the question the code where the redirect is done I update.

  • Thanks Ricardo! I updated the code for you!

  • I updated the reply @Andersoncosta, see if it resolves.

  • It Works! Ricardo Ball Show! Vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.