I'm trying to write code that will display a URL link if there one present from a form sumbmission;
If > [a link exists]
then [display the text 'more info' with the href link wrapped around it]
I've confused myself mixing wordpress and php, and can't quite get it. Any help would be great.
This question isn't very specific, but the pseudo-code I can offer is this:
<?php if (isset($_GET['url'])): ?>
Read more
<?php endif; ?>
Are you looking to do something like this to the comments displayed on a post?
Comment: "I like https://www.google.com/" becomes "I like more info".
If that's the case, perhaps adding a filter to functions.php to search for and replace URL might do the trick:
// define the get_comment_text callback
function filter_get_comment_text( $comment_comment_content, $comment, $args ) {
// Regular expression to find URL
$pattern = '/(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)/i';
// Replace url with linked "more info"
$replacement = 'more info';
// Find matches & replace
$newcomment = preg_replace($pattern, $replacement, $comment_comment_content);
// Return the comment
return $newcomment;
};
// add the filter
add_filter( 'get_comment_text', 'filter_get_comment_text', 10, 3 );
Related
I have never programmed in php before but I just found out that i need to edit the wp-admin page of a word press site and it MUST be done in php.
Actually what i need to do is change the color of a specific word.For example I need "cars" to be always on red. In jQuery that could be something like this:
$('p:contains("cars")').css('color', 'red');
Can anyone help me to write this in php please
You can use strpos and do something like that :
$text = "some text containing cars word";
if (strpos($text , 'cars') !== false) {
$style = 'style="color:red"';
}
else {
$style = "";
}
echo "<p ".$style.">".$text."</p>";
If the text contains "cars" word, strpos() will return the position of the word in the text. Else it will return false.
if you want to replace just words cars colored by red use preg_replace with flag g in pathern Regex to match all the words (cars) in the paragraph and replace them with red colored span. if you want change all paragraph use the code suggested by Titi
<?php
$paragraph = "<p>we all know cars faster than bikes!<p>";
echo preg_replace('/(cars)/g', '<span style="color:red;">$0</span>', $paragraph);
//<p>we all know <span style="color:red;">cars</span> faster than bikes!<p>
?>
It seems that you can use js script in the WordPress admin_enqueue_scripts.
Place your script into your plugin folder and then use it like that :
function my_enqueue($hook) {
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts', 'my_enqueue');
This way, it calls a PHP function that calls a js file
Here is the official documentation about that : https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
There are some posts about that at the bottom of the page.
How can i call in php words from url?
Example 1
http://myurl.com/keyword-one
I want to display "Keyword One" as page title in php
Example 2
http://myurl.com/keyword/keyword-two
I want to display "Keyword Two" as page title in php
Thank you! :-)
the simplest solution in those cases would be using (untested code, but should work):
<?php
//get path
$urlPath = $_SERVER["REQUEST_URI"];
//get last element
$end = array_slice(explode('/', rtrim($urlPath, '/')), -1)[0];
//replace dashes with spaces and display
echo ucwords(str_replace('-',' ',$end));
I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!
This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>
So I have three pages one that is the index page. One that writes the data from a form inside the index page to the database. And one that gets data from the database and echos out a html table with the data inside.
Currently if you write a link in the form. It will just come out as text. I would like the whole link to be like [link].
so say if I wrote this onto the form:
Look at this: www.google.com or Look at this: https://www.google.com
it would come out like this in html
Look at this: www.google.com
How could I go about doing this?
Okay so the html is:
<form class="wide" action="Write-to.php" method="post">
<input class="wide" autocomplete="off" name="txt" type="text" id="usermsg" style="font-size:2.4vw;" value="" />
</form>
in which the user would write:
"Look at this: www.google.com or Look at this: https://www.google.com"
This would then get sent to the database through Write-to.php.
$sql="INSERT INTO social (comunicate)
VALUES
('$_POST[txt]')";
}
this then gets written back into the database:
$result = mysqli_query($con,"(select * from social order by id desc limit {$limit_amt}) order by id asc");
while($row = mysqli_fetch_array($result))
{
echo "<tr div id='".$i."' class='border_bottom'>";
echo "<th scope='col'>";
echo "<span class='text'>".htmlspecialchars($row['comunicate'])."</span><br />";
echo "</th>";
echo "</tr>";
}
Just try:
echo(''.$your_url_variable.'');
Update:
The OP really wanted to detect url's in a string. One possible solution could be filter it using a regular expression. This code could help:
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "{$url[0]} ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
?>
Source: http://css-tricks.com/snippets/php/find-urls-in-text-make-links/
There are quite a few things you need to worry about when displaying user supplied (tainted) data.
You must ensure that all the data is sanitised -- never ever just echo the content, look into htmspecialchars and FILTER_VALIDATE_URL for example:
function validateUrl($url) {
return filter_var($url, FILTER_VALIDATE_URL);
}
What you are attempting to do is convert a string into a link, for example you can write a function like this:
function makeClickable($link) {
$link = htmlspecialchars($link);
return sprintf('%s', $link, $link);
}
You can use string concatenation as well, but I wouldn't do that in my view code. Just personal preference.
Take a look at the urlencode function, it will certainly come in handy.
I would also recommend you read about cross site scripting
Please note that I am not making any implementation recommendations, my aim is just to show you some contrived code samples that demonstrate making a string "clickable".
Update:
If you would like to make links clickable within text, refer to the following questions:
Best way to make links clickable in block of text
Replace URLs in text with HTML links
save the hyperlink in db and retrieve as a string by sql query
like:
select link from table_name where index = i
and save link as: whaatever here
and print it
Use this
echo '' . $res['url'] . '';
Good day everyone,
I'm very new with phpquery and this is my first post here at stackoverflow for a reason that i cant find the correct for syntax for the phpquery chaining. I know someone knows what i been looking for.
I only want to remove the a certain div inside a div.
<div id = "content">
<p>The text that i want to display</p>
<div class="node-links">Stuff i want to remove</div>
</content>
This few lines of codes works perfect
pq('div.node-links')->remove();
$text = pq('div#content');
print $text; //output: The text that i want to display
But when I tried
$text = pq('div#content')->removeClass('div.node-links'); //or
$text = pq('div#content')->remove('div.node-links');
//output: The text that i want to display (+) Stuff i want to remove
Can someone tell me why the second block of code is not working?
Thanks!
The first line of code will only work if your trying to remove the class from div.node-links, it won't remove the node.
If you are trying to remove the class you need to change it from:
$text = pq('div#content')->removeClass('div.node-links');
// to
$text = pq('div#content')->find('.node-links')->removeClass('node-links')->end();
which will output:
<div id="content">
<p>The text that i want to display</p>
<div>Stuff i want to remove</div>
</div>
As for the second line of code.. I'm not exactly sure why it is not working, it seems like your not selecting .node-links but I was able to get the desired results using these.
// $markup = file_get_contents('test.html');
// $doc = phpQuery::newDocumentHTML($markup);
$text = $doc->find('div#content')->children()->remove('.node-links')->end();
// or
$text = pq('div#content')->find('.node-links')->remove()->end();
// or
$text = pq('div#content > *')->remove('.node-links')->parent();
Hope that helps
Since remove() does not take any parameter, you can do:
$text = pq('div#content div.node-links')->remove();