Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a WP single page portfolio that loads post content with AJAX. I read that WP doesn't need #! URLs to make it crawlable by Googlebot. If I understand it correctly, since Googlebot won't interpret ajax, it will just follow the link and index the article page, Am I right?

So I removed #! from my URLs and when a post is opened I update them with .pushState.
It's working fine, but if I hit F5 or if I try to load a specific URL (not the home) I fall into the page of the post (single.php).

To prevent it, I added a javascript redirection in single.php that leads users to the homepage. Then I need to open the content of the article on the home page.

Here is the address of my site: http://www.youpiemonday.com/ (the redirection isn't online yet).

How can I tell the browser "if you're coming from this article page, click on the corresponding thumbnail" (the click will load the content)?

I'm digging around this but I have no idea how to make it work...

<?php   
    $referer = $_SERVER['HTTP_REFERER'];
    // search for the link correponding to the referer's URL and trigger a click on the href
        if ($referer == 'SomeUrl') {
           $('TheCorrespondingTumbnail href').trigger('click');
        }
?>

I'm not really a programmer so any help would be very appreciated. And if you can comment your code, that would be invaluable to me.


Here is what the html looks like for each thumbnail:

<div class="ProjectWrap">
       <a href="<?php the_permalink(); ?>" class="mosaic-overlay"><?php the_post_thumbnail(); ?></a>
       <a href="<?php the_permalink(); ?>" class="mosaic-backdrop">
            <div class="contentProject">
                <h4 class="ProjectTitle"><?php the_title(); ?></h4>
            </div>
       </a>
</div>

Update: I try to write a more effective code here.

<script type="text/javascript">

    $referer = document.referrer; // where we come from
    $RefHref = $('.ProjectWrap').find("a").attr("href"); // the href in the thumbnail

        if ($referer == $RefHref){
            $('.ProjectWrap a').trigger('click');
        }

</script>
share|improve this question
You may be using WordPress but I am not sure this is a WordPress question. It sounds like a pure Javascript and PHP question to me which would be better placed on Stack Overflow. – s_ha_dum Dec 14 '12 at 15:32
Thanks, yeah I thought here was for anything WP related but it's clearly more a javascript question. Do you know if I can swap this to SO or should I create a new thread there? – wyem Dec 14 '12 at 15:50
Mods can move it I think. Possibly very high rank users can too. – s_ha_dum Jan 24 at 15:44

closed as off topic by toscho Mar 25 at 3:26

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

You can't trigger a javascript click event with PHP. PHP renders the document and sends it to your visitor, while javascript is used by browsers to make changes to the document on the visitors machine.

Instead, check the document.referrer property using javascript, and trigger your click event if it matches that URI.

share|improve this answer
Ok thanks, as I learn I tend to mix everything together... I just updated the post with something... Is it close to something valid? :) – wyem Dec 14 '12 at 16:11
looks ok, but you should test it yourself – One Trick Pony Dec 14 '12 at 17:26
Thank you, well it's not working so if you think the code is OK I may be targeting the link the wrong way. – wyem Dec 14 '12 at 18:41
is there any referrer set? Did you check the value of $referer? – One Trick Pony Dec 14 '12 at 19:43
Yes the referrer is OK but $RefHref is undefined. – wyem Dec 14 '12 at 19:50
show 9 more comments

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