I want to track internal URL that brought the URL to the next page or exit page - wordpress

I want to track the URL of the current page, then pass it to a URL variable to the next page.
For example
https://www.mywebsite.com
https://www.mywebsite.com/about/?fromsource=https://www.mywebsite.com
https://www.mywebsite.com/about/careers/?fromsource=https://www.mywebsite.com/about
https://www.mywebsite.com/contact/?fromsource=https://www.mywebsite.com/about/careers
So everytime someone moves to the next page, the URL of the previous page is add to a URL variable in the next page.

I'm not sure if this is the right way. I would use Google Analytics API referral data. But you can create the following shortcode:
function get_current_page_link () {
global $wp; //get global wp query
return home_url( $wp->request ); //get the url of the current page
}
add_shortcode('get_current_page_link ', 'get_current_page_link ');
If you want to add the code via PHP code:
About
If you want to use it in your editor you can add url's as such:
https://www.mywebsite.com/about/?fromsource=[get_current_page_link]
The editor in the text mode, it will show up like this:
About

Why not use document.referrer?
So e.g. create a new variable in your web-analytics system and use the following js code to populate it:
function includeReferrer() {
var ref = document.referrer,
loc = document.location.href,
newLoc;
if (loc.includes('?') != -1) {
newLoc = loc + "&fromsource=" + ref;
} else {
newLoc = loc + "?fromsource=" + ref;
}
}
In this case, you are not overwriting the actual URL, but instead create a new string which includes the requested parameter and can be used for any kind of tracking purpose.

Related

Grab a query string and add the query string to the href in a Wordpress post

I'm trying to figure out how to grab a query string from the address bar and append it to links in a Wordpress post. I don't want to use a plugin. I would rather find away to do this without a plugin. In non-wordpress pages, I use
. urldecode($_SERVER["QUERY_STRING"]) .
but php isn't allowed in posts and widgets in wordpress. How can I grab the query string and add it to the href in a Wordpress post? Can someone help?
http://php.net/manual/en/function.parse-url.php
<?php
$parts = parse_url($_SERVER['REQUEST_URI']);
var_dump($parts, $parts['query']);
//or you can always access parameters
var_dump($_GET);
If you can add a javascript to a page you can do:
function overwriteLinks () {
var path = document.location.search;
var links = document.getElementsByTagName("a");
for (i=0, l=links.length; i < l; i++) {
var link = links[i];
var newPath = link.href.replace(link.hash, '') + path + link.hash ;
link.href = newPath;
}
}
overwriteLinks ();
That would overwrite all links the way you want and preserve hash values as well

Composite C1 - How do I redirect to a 404 within an InlineMethodFunction

I have a Composite site where the URL structure is like so:
site.com/products/1234/product-name
The bold part is the page location. 1234 is the Product ID and then product-name is just a slug.
I register PathInfo and I'm using 1234 to read a SQL Database and return information to show on the site. I'm doing that inside an inline C# function called productDetails which returns an XElement to the XSLT Function.
Sometimes, we will discontinue a product or the ID will not exist in our database. What I'd like to do in those cases is to do one of the following
Return a 404
Redirect to my 404 with a URL
I then want to email out a notice (or at least log the error).
Currently, if the product doesn't exist, I just return an empty <product> element.
I have tried the following code inside the InlineMethodFunction > 'productDetails method, but the Response Object doesn't seem to be available. IDEALLY I want to set the Response Type to 404 instead of redirect. Any ideas?:
public static class InlineMethodFunction
{
public static XElement productDetails( int BulkProductId ) {
ProductContext db = new ProductContext();
var products = db.ProductDetailsByBulkID(BulkProductId).ToList();
if (products.Count < 1)
{
// THIS doesn't work
Response.Redirect("~/404?ProductCode=" + BulkProductId.ToString());
HttpContext.Current.Response.Redirect("~/404?ProductCode=" + BulkProductId.ToString());
// ERROR RETURNED The name 'HttpContext' does not exist in the current context
}
//
// rest of code
//
}
}
As it turns out, I was only able to accomplish this in an External function. Kudos to Pauli Østerø!
For those interested... here's the actual code used:
if(products.Count < 1){
HttpResponse response = HttpContext.Current.Response;
response.StatusCode = 404;
response.Status = "404 Not Found";
//
// Send mail to let someone know
//
}
Rather than redirect, I just set the 404 message and the URL stays intact, but with a 404.

Send Gravity Forms data to redirection page

I have a very simple form created with Gravity Forms;
It submits two numbers and then redirects to a different result page.
How do I retrieve those two numbers on the result page?
add_filter("gform_confirmation_4", "custom_confirmation", 3, 4 );
function custom_confirmation($confirmation, $form, $lead, $ajax)
Gives a custom confirmation. Each field value can be retrieved by using $lead[{field ID}]
I have a solution for this based on using a combination of form submission hooks and the GForms API. It's a horrible plugin so I apologise for the messiness of the logic flow. It's important to use the framework methods rather than processing the data yourself since there are a good amount of hacks and shonky things going on in there to correctly match field IDs and so forth.
I will provide a solution to pass a submission from one form to pre-populate another. Changing the destination for POST data is pretty straightforward, they have an example for it on their gform_form_tag hook documentation page. Yes, that really is the only way of doing it.
Without further ado here is the code. I've set it up to work off form configuration to make things simpler for the end user, so it works like this:
Select "allow field to be populated dynamically" in your destination form field's advanced settings and choose a parameter name for each.
Add matching CSS classes on the source fields of the other form(s) to setup the associations.
Add a CSS class to the source forms themselves so that we can quickly check if the redirection is necessary.
.
$class = 'GForms_Redirector';
add_filter('gform_pre_submission', array($class, 'checkForSubmissionRedirection'), 10, 1);
add_filter('gform_confirmation', array($class, 'performSubmissionRedirection'), 10, 4);
abstract class GForms_Redirector
{
const SOURCE_FORMS_CLASS_MATCH = 'submission-redirect';
const DEST_PAGE_SLUG = 'submit-page-slug';
const DEST_FORM_ID = 1;
protected static $submissionRedirectUrl;
// first, read sent data and generate redirection URL
function checkForSubmissionRedirection($form)
{
if (false !== preg_match('#\W' . self::SOURCE_FORMS_CLASS_MATCH . '\W#', $form['cssClass'])) {
// load page for base redirect URL
$destPage = get_page_by_path(self::DEST_PAGE_SLUG);
// load form for reading destination form config
$destForm = RGFormsModel::get_form_meta(self::DEST_FORM_ID, true);
$destForm = RGFormsModel::add_default_properties($destForm);
// generate submission data for this form (there seem to be no hooks before gform_confirmation that allow access to this. DUMB.)
$formData = GFFormsModel::create_lead($form);
// create a querystring for the new form based on mapping dynamic population parameters to CSS class names in source form
$queryVars = array();
foreach ($destForm['fields'] as $destField) {
if (empty($destField['inputName'])) {
continue;
}
foreach ($form['fields'] as $field) {
if (preg_match('#(\s|^)' . preg_quote($destField['inputName'], '#') . '(\s|$)#', $field['cssClass'])) {
$queryVars[$destField['inputName']] = $formData[$field['id']];
break;
}
}
}
// set the redirect URL to be used later
self::$submissionRedirectUrl = get_permalink($destPage) . "?" . http_build_query($queryVars);
}
}
// when we get to the confirmation step we set the redirect URL to forward on to
function performSubmissionRedirection($confirmation, $form, $entry, $is_ajax = false)
{
if (self::$submissionRedirectUrl) {
return array('redirect' => self::$submissionRedirectUrl);
}
return $confirmation;
}
}
If you wanted to pass the form values someplace else via the querystring then you'd merely need to cut out my code from the callback and build your own URL to redirect to.
This is a very old question, now you can send it using a Query String on the confirmation settings.
They have the documentation on this link:
How to send data from a form using confirmations
Just follow the first step and it will be clear to you.

Setting the CSS class to "current" for the navigation menu in ASP Classic

I have an include file that contains the primary navigation menu for the site. I want to be able to set a CSS class for the current page. This is what I've been able to put together so far:
public function GetFileName()
Dim files, url, segments, current
'get then current url from the server variables
url = Request.ServerVariables("path_info")
segments = split(url,"/")
'read the last segment
url = segments(ubound(segments))
GetFileName = url
end function
if GetFileName = "index.asp" then
current = "current"
else
current = ""
end if
I'm thinking that a Select Case statement would be the thing to use in this scenario, I'm just not sure how to go about constructing it? Thanks in advance!
You'll need to add the definition of Iif to your code (from here: http://support.microsoft.com/kb/219271 )
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function
I assume you have something like this.
<li>Click me to go somewhere</li>
You can do this:
<li>Click me to go somewhere</li>
You could do it in jquery
jQuery add class based on page URL
$(function() {
var loc = window.location.href;
if(/index.asp/.test(loc)) {
$(body).addClass('index');
}
});

Query strip is removed from open graph url

In relation to this question: Dynamic generation of Facebook Open Graph meta tags
I have followed these instructions but the api seems to remove my query string so that the url passed into the aggregation contains none of my dynamic information. If I enter the url with the query string into the debugger it doesn't remove it and works fine. I can confirm my og:url meta tag does also contain the same query string not just the base url. What am I doing wrong?
I was having a similar issue and solved it like this:
So assuming you're doing your post request like it shows in the tutorial, youre Javascript probably looks something like this:
function postNewAction()
{
passString = '&object=http://yoursite.com/appnamespace/object.php';
FB.api('/me/APP_NAMESPACE:ACTION' + passString,'post',
function(response) {
if (!response || response.error) {
alert(response.error.message);
}
else {
alert('Post was successful! Action ID: ' + response.id);
}
}
);
}
And since you say you want to generate meta tags dynamically, you're probably adding a parameter to the url (passString) there like so:
passString = '&object=http://yoursite.com/appnamespace/object.php?user=' + someuser;
This is wrong.
What you need to do is to make the url a 'pretty url' and use htaccess to decipher it. So:
passString = '&object=http://yoursite.com/appnamespace/object/someuser';
Then your htaccess file will tell your site that that url actually equates to
http://yoursite.com/appnamespace/object/object.php?user=someuser
Then you can use GET to store the user parameter with php and insert it however you like into your meta tags.
In case youre wondering, in the og:url meta tag's content will be:
$url = 'http://yoursite.com/appnamespace/object/object.php?user=' . $_GET[$user];
Does that help?

Resources