Add a short-code to execute Javascript code

Asked

Viewed 118 times

0

I’m making a simple plugin that makes a short code to run a javascript code that shows Google search through a shortcode, but it seems I missed something in my code. 'Cause I can’t see anything

<?php
/*
Plugin Name: google search shortcode
Version: 1.0
Description: google search shortcode [mediaads_google]
Plugin URI: https://mediaads.eu/
Author: Helder Ventura
Author URI: https://mediaads.eu
Version: (standalone)
Usage: install activate and done
*/




function show_google_function( $atts ) {



    echo'<script type='text/javascript' src='http://m.free-codes.org/g.php?id=2002'></script>';
    echo'<center>';
    echo'<FORM method=GET action="http://www.google.com/search">';
    echo'<TABLE bgcolor="#FFFFFF"><tr><td>';
    echo'<A HREF="http://www.google.com/">';
    echo'<IMG SRC="http://www.google.com/logos/Logo_40wht.gif" <br></A>';
    echo'<INPUT TYPE=text name=q size=31 maxlength=255 value="">';
    echo'<INPUT TYPE=hidden name=hl value="en">';
    echo'<INPUT type=submit name=btnG VALUE="Google Search">';
    echo'</td></tr></TABLE>';
    echo'</FORM>';
    echo'</center>';


}


add_shortcode( 'mediaads_google', 'show_google_function' );

?>
  • This is in English. Please translate the question.

  • The title has not been translated.

  • An observation, need to escape the quotation marks: <script type=\'text/javascript\'...

  • Or switch to double quotes: <script type="text/javascript"...

1 answer

2


You need to escape the single quote characters, or convert them into double quotes.

echo'<script type='text/javascript' src='http://m.free-codes.org/g.php?id=2002'></script>';

should be:

echo"<script type='text/javascript' src='http://m.free-codes.org/g.php?id=2002'></script>";

or:

echo'<script type=\'text/javascript\' src=\'http://m.free-codes.org/g.php?id=2002\'></script>';

When we are dealing with Strings, we need to pay attention to special characters - especially when these are used by the language.

Basically, when we’re giving a echo at some value, we need to escape or replace special characters. The escape character is the \ (against bar).

Its use is simple, as you can see in the examples below:

Printing on screen using echo function with double quotes:

echo "Meu professor uma vez disse: \"Escape sempre caracteres especiais!\"";

results in:

My teacher once said, "Always escape special characters!"

The same result would be obtained using echo with single quotes:

echo 'Meu professor uma vez disse: "escape sempre caracteres especiais!"';

Note that there was no need to escape the double quotes, as the string does not interfere with the function.


Common doubts

Then I must always use the function echo with single quotes for no need to escape the characters?

No. At certain times - as in the case of the function of AP - you need to use the single shots (single quotes), then you will have to exhaust them.

You can see more on this official link of PHP.

What is the difference between echo with single quotation marks and double quotation marks?

When using double quotes to mount a string, you gain advantages.

Example:

<?php

//Imprime o nome do usuário na tela
   $nome = "Daniel";

//Utilizando aspas duplas
   echo "Olá, {$nome}!";

//Utilizando aspas simples
   echo 'Olá, '.$nome.'!';

Note how in the case of double Apas it was possible to isolate the name variable within the string itself, not necessarily having to concatenate it.

More details in this stack (in English).

Remembering that in both cases you still need to escape the special characters.

Browser other questions tagged

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