Add Parameters in the Wordpress URL

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 some parameters at the end of the Links. Ex:

Link == > otherite.com

I want to add the following now:

Link == > otherite.com?src=urldinamicadowordpress

So I can see the source of these clicks and which article is converting more clicks.

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

Link == > otherite.com?src=urldinamicadowordpress|mobile

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

Redirect Code

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;
}
}
    
asked by anonymous 05.11.2016 / 14:08

1 answer

1

How to do this depends a bit on how the 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 put to 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 wp_is_mobile()

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

This solution works only if you do not have full page caching. If you do you will 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 duplicate bars at the end of the URL. I'm considering that $ meta-> meta_link never has parameters, it's always a simple URL. If you need to change this code to test by parameters and use ? or & properly.

    
06.11.2016 / 01:59