can this code snippet be modified to reveal html coding?
<?php $_option = $this->getOption() ?>
<?php echo $this->htmlEscape($_option->getTitle()) ?>
it is showing a title but in that title i have an href:
link
instead of it appearing as a link it just shows me the code.
i'd like it to be a clickable link.
possible?
Yes, just remove the $this->htmlEscape() and leave the $_option->getTitle()
The escape function is to provide just the functionality of not being able to use (or inject) html commands or javascript.
Related
I'm in the process of making a site that has a lot of different URL's all sharing the same design. I'm trying to make the titles display on each site because they're all unique, where as everything else is the same for the most part. I tried using this code:
<script type="text/javascript">
<!--
document.write(document.referrer);
// -->
</script>
On some browsers this would display the URL at the bottom of the webpage, but on others it wouldn't show at all, I tried to grab the data from this and move it to where I would like the content to go but I wasn't able to select it at all.
I'm using wordpress as my CMS and I looked into the editor to see if there was something I could do there and realized its all php - something I've never used before. I searched if there was a way to do this in wordpress but turned up with nothing.
I'm assuming this is where the php code should be
<h1 class="site-title">here</h1>
I'm assuming I'd need something similar to the php echo in the href where it says 'here'. Just to try I put the script in the anchor tag and not surprisingly the whole site stopped working because of an error. So I'm stuck.
Is there a way in wordpress that I can pull the title tag from the header into the html?
Here's a simple script to get the title of the page.
var title = $("title").text();
$("body").html("<h1>" + title + "</h1>");
The first line is getting the text portion of the title tag. The second line just replaces all the contents of your body tag to only contain the title extracted. Modify the second line to fit what you need. The important part you need to know is the first line.
I believe this was answered in this question
To get the path, you can use:
var pathname = window.location.pathname; // Returns path only
var url = window.location.href; // Returns full URL
If you want the current page title use
<?php the_title(); ?>
Or if you want the meta title use
<?php wp_title(); ?>
They're both WordPress PHP functions.
Assuming you want the meta title (from the header) use
<h1 class="site-title"><?php wp_title(); ?></h1>
Here's the docs https://developer.wordpress.org/reference/functions/wp_title/
Hope that helps!
I want to create a html list item with php for a webpage with dynamic content by printing the tags. I want to do something like this
<?php
...
...
print(<li>$Row['Name']</li>);
...
...
?>
but it doesnt work. How can I do that ?? Thanks for your help.
First, the page's file name must have .php extension, as well as having PHP being enabled on your web server.
Second, add something along the lines of this in to your page:
<?php
# ...
print("<li>".$Row['Name']."</li>");
# ...
?>
Note: I don't know what else content you have but you can do it yourself by adding quotes around html tags carefully as I did in my example.
print "<li>$Row['Name']</li>";
If you mean that you want to see the tags on the browser page instead of them being actual, functioning tags, then:
print htmlspecialchars("<li>$Row['Name']</li>");
try this:
echo "<li>$Row['Name']</li>";
I'm trying to create a facebook share button in each of my post, and the share content will be dynamic, which mean I will be able to customize its thumbnail, title and description for each of the post.
below is the code that I use(I'm using advance custom field plugin in wordpress by the way):
<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php the_field(videotitle); ?>&p[summary]=<?php the_field(video_description); ?>&p[url]=<?php echo get_permalink(); ?>&p[images][0]=http://img.youtube.com/vi/<?php the_field(youtube_thumb); ?>/maxresdefault.jpg','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)" rel="nofollow"></a>
///////////////////////////////////////////////////////////////////////////////////////////////////////
Below is the php that will echo out my content from my CMS:
<?php the_field(videotitle); ?>
<?php the_field(video_description); ?>
<?php echo get_permalink(); ?>
The code works fine, but I noticed when I enter the the title/description too long or use special characters in my post the button stop working.
How should I overcome this? I'm still very new to php, please explain in layman's term if possible and thank you in advance.
The problem is most likely caused by passing in unescaped special characters into a direct javascript call.
Right now, you have the following javascript executing when the link is clicked:
window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php the_field(videotitle); ?>&p[summary]=<?php the_field(video_description); ?>&p[url]=<?php echo get_permalink(); ?>&p[images][0]=http://img.youtube.com/vi/<?php the_field(youtube_thumb); ?>/maxresdefault.jpg','sharer','toolbar=0,status=0,width=548,height=325');
You are passing in several PHP variables, which may alter the format of your javascript. For example, let's say the_field(videotitle); returns Maria's Video. If you note, your string has a quote in it due to Maria's.
Now, you if pass this title into your javascript, you're going to have an un-escaped quote, causing a JS error, because it will output like this:
... [title]=Maria's Video ...
To address this, you must format out PHP output to ensure that it will not affect the JS code. In my example, you can encode the outputted strings using the urlencode function included with PHP, like this:
<?php urlencode(get_the_field(videotitle)); ?>
Just remember that passing PHP variables into javascript CAN alter the syntax of your javascript function. If the final javascript function contains syntax errors caused by the PHP output, it will not run.
You can see the javascript errors on the page you are debugging by hitting F12 in your browser and viewing the Console tab.
In ckeditor, I'm sharing code snippets such as php, html, etc. So for example I would share this:
<?php
//Something here...
?>
Which appears in source view as this:
<?php
//Something here...
?>
When I save it to mysql, the code stays as it should. But when I load up the code, ckeditor changes the code in source view to:
<!--?php
//Something here...
?-->
Are there any solutions to fix this?
You must type your php codes in source view otherwise it's become html code
and for protect it from ckeditor use this code in your config.js file:
config.protectedSource.push(/<\?[\s\S]*?\?>/g);
I added config.protectedSource as Kiyan noted and that works for working within the editor. However to view the page as an article including whatever you added in php I had to add the following in my output article/page:
textarea is the field stored in the mysql database.
<?php echo eval('?>' . utf8_encode($row['textarea']) . '<?php '); ?>
In the editor if I added some php like <?php include ("whatever.php"); ?>, it nicely includes the whatever.php file.
How would one go about showing PHP code on user end. Sort of like w3School does?
Having lets say a grey area div, and then showing the code in there without activating it?
You can use html entities <?php in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
Use <pre> or <code> tags to wrap your code.
Take a look at http://php.net/manual/en/function.highlight-string.php to further see how you can make the code look pretty.
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
Something like:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
Simply you can use following code to display php code on webpage.
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
<?php print('This is php code.'); ?>
The first step is to not wrap that code in PHP tags. So instead of this:
<?
var sample = "code";
?>
You would have this:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre tags to get rid of HTML-ish formatting:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre tags (or any other tags you want to use for displaying code, such as div) as you see fit.
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettify often comes highly recommended.
Typically this is done by showing code within <pre> or <code> tags.
You can use this template........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
It will show the source code and output following.
use the header function of php, this will rea
<?php
header("content-type: text/plain");
?>
The PHP code will just be a string that you can echo or print onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.
Ex:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";