Mo Beigi
Mo Beigi

Reputation: 1765

Need to insert javascript into php code [Wordpress Website]

For my wordpress website I am trying to remove the Google timestamp that is currently in my SERP (search page result description.)

To do this I must use javascript to get the time and report it back.

You may want to refer to this link: http://www.andrewkeir.com/remove-wordpress-post-datestamp-timestamp-google-serps/

function twentyten_posted_on() {
printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
    'meta-prep meta-prep-author',
    sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
        get_permalink(),
        esc_attr( get_the_time() ),
        get_the_date()

    ),

In the above code get_the_time() and get_the_date() are the parts are want to replace with the following javascript:

<script language="javascript" type="text/javascript">document.write("<?php the_time('F jS, Y') ?>");</script>

Whats the PHP code to do this! I have tried a million things which all result in error's so I think I may be overlooking something.

Upvotes: 3

Views: 1467

Answers (2)

Marc B
Marc B

Reputation: 360572

Ok, since we must be bow before the Altar of SEO...

a) Generate your JS snippet:

$date = get_the_time('F jS, Y');
$js = <<<EOL
<script language="javascript" type="text/javascript">document.write('$date');</script>
EOL;

b) Insert that snippet into the link:

sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
        get_permalink(),
        $js,
        get_the_date()

Upvotes: 2

Brent Friar
Brent Friar

Reputation: 10609

I don't think you understood the instructions properly. You shouldn't be doing anything to the PHP function that creates the datestamp. You need to replace the piece of code that calls the function in your template files.

There are 3 files you need to look at in your template - index.php, single.php, and archive.php. In those files look for the part of the file that displays the date. It looks like your template uses the function twentyten_posted_on to display the date so look for that. Once you find it, replace the whole thing with <script language="javascript" type="text/javascript">document.write("<?php the_time('F jS, Y') ?>");</script>

If you can't find it, post the contents of index.php so we can see how your template works.

Upvotes: 0

Related Questions