I'm currently using this code:
$blog= file_get_contents("http://powback.tumblr.com/post/" . $post);
echo $blog;
And it works. But tumblr has added a script that activates each time you enter a password-field. So my question is:
Can i remove certain parts with "file_get_contents"?
Or just remove everything above
anything will work!
Thank you for your help!
(could i possibly kill a whole div so it wont load at all?)
You could try something like this, assuming the div you are trying to remove has a specific id and/or class:
$blog= file_get_contents("http://powback.tumblr.com/post/" . $post);
$blog = preg_replace('!<div\s+id="div-id"\s+class="div-class">.*?</div>!is', '', $blog);
echo $blog;
The post I found this in (http://stackoverflow.com/questions/1114916/how-can-i-remove-an-html-element-and-its-contents-using-regex) notes that if there is a nested div, this will fail.
Regardless, preg_replace or regex is your best bet here.
Related
I'm looking for php code or function which can help me to search and find first matching URL, based on specific pattern, from a wordpress post and echo the same url in the same post where it's necessary instead of modifying parent url.
Example Case:
URL:https://example.com/education_system_comparison.html
when I open this page, afer title of the post, there is description of this post, here I want to search from description to find matching url that must starts from my desired domain name like https://some-url-is-here/3978732978937298.html and ends with .html
If anyone here can help me to create a function or use any filters those can work with wordpress or php, it would be so helping and must appreciated. Thank you so much.
I found the solution what I was looking for and it's working perfectly fine on single post or on custom page.
Here is function, I've added in functions.php of my WordPress theme;
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return ''; }
so I can call above function by using following code and it work 100% fine, example code:
<?php
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$start = ' https://some-url-is-here/';
$end = '"';
$output = getBetween($content,$start,$end);
echo $start.$output; ?>
It gives output like below based on first matching url and stop immediately
https://some-url-is-here/3978732978937298.html
On the other hand, once I applied the same code throughout my website so I can get every matching url from each and every published post to use it where necessary on the same post. As I save changes, my server got halted coz there were hundreds of httpd requests started processing and server went down immediately.
I'm not sure what exactly is going wrong here, If anyone can guide me and help me what exactly wrong here so it'll be much appreciated and any suggestion or fix, Thanks.
I'm trying to take the currently logged in user id on my website and add it to the end of the URL to display in an iframe (iframe is for a third party website)
I have stumbled upon this code;
function makeClickableLink($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1\\2', $text);
return $text;
}
$user_id = get_current_user_id(); // getting current user id from wordpress
$text = "http://example.com/something/something/?ba=310139&gd=927691&sid=$user_id"; // that's the link to a website i'm trying to iframe with my $user_id at the end
echo makeClickableLink($text);
http://example.com/something/something/?ba=310139&gd=927691&sid=1
So this code outputs the link that I put into the $text and replaces $user_id with the currently logged in users id which is so far exactly what I'm looking for.
Now.. Is there a way to iframe this link by modifying the above code ? (i have a plugin that allows me to use the iframe tags in wordpress before someone mentions that i can't use it there) Or another different way to do it? I need the above URL but in an iframe. I'm sure this is fairly easy to do however I cannot wrap my head around it (very new to all this but trying my best to learn :))
Any help would be greatly appreciated! Thanks!
this is probably a VERY stupid question but I'm a bit new into this so please help me out. I have written a mini code using instagram API that loads photos from a specific location, using the location id. Now what I want to achieve is that use this for a wordpress website i'm building that lists some hotels and restaurants. I've defined the location id as a custom field, so I need to use the following code to call it:
<?php echo get_post_meta($post->ID, 'Instagram', true); ?>
And my url to call the photos is the following:
<?php $result = fetchData("https://api.instagram.com/v1/locations/LOCATION-ID-HERE/media/recent/?access_token={$accessToken}&count=6");?>
The above code works if i insert a location id manually. What I want to achieve is insert the above code into the "location-id-here" part. I guess this is quite simple but I'm kind of confused as both are php functions.
Any help would be greatly appreciated!
The parameter you're putting in fetchData() is just a string, so you can concatenate output from another function into that string with the . operator:
<?php
$result = fetchData("https://api.instagram.com/v1/locations/" . get_post_meta($post->ID, 'Instagram', true) . "/media/recent/?access_token={$accessToken}&count=6");
?>
im trying to figure out how i can show the last 3-5 or so pages within my site a person has visited. I did some searching, and I couldn't find a WP plugin that does so, if anyone knows of one, please point me in that direction :) if not, I'll have to write it from scratch, and thats where i'll need the help.
I've been trying to understand the DB and how it works. I'm assuming that this is where the magic will happen, with PHP, unless there is a javascript option using cookies to do it.
Im open to all ideas :P & Thank you
If i were to code such a plugin, i'd use the session cookies to populate an array via array_unshift() and array_pop(). it'd be as simple as :
$server_url = "http://mydomain.com";
$current_url = $server_url.$_SERVER['PHP_SELF'];
$history_max_url = 5; // change to the number of urls in the history array
//Assign _SESSION array to variable, create one if empty ::: Thanks to Sold Out Activist for the explanation!
$history = (array) $_SESSION['history'];
//Add current url as the latest visit
array_unshift($history, $current_url);
//If history array is full, remove oldest entry
if (count($history) > $history_max_url) {
array_pop($history);
}
//update session variable
$_SESSION['history']=$history;
Now i've coded this on the fly. There might be syntax errors or typos. If such a mistake appears, just put a notice and i'll modify it. The purpose of this answer is mostly to make a proof of concept. You can adapt this to your liking. Please note that i assume that session_start() is already in your code.
Hope it helps.
===============
Hey! Sorry about the late answer, i was out of town for a couple of days! :)
This addon is to answer your request for a print out solution with LI tags
Here's what i'd do :
print "<ol>";
foreach($_SESSION['history'] as $line) {
print "<li>".$line.</li>";
}
print "</ol>";
Simple as that. you should read the foreach loop here : http://www.php.net/manual/en/control-structures.foreach.php
As for the session_start();, put it before you use any $_SESSION variables.
Hope it helped! :)
I'm going to update and translate the code above for WordPress 5+ because the original question has the wordpress tag. Note that you don't need session_start() anywhere.
Here it goes, add the code below to your singular.php template (or single.php + page.php templates, depending on what you need):
/**
* Store last visited ID (WordPress ID)
*/
function so7035465_store_last_id() {
global $post;
$postId = $post->ID; // or get the post ID from your template
$historyMaxUrl = 3; // number of URLs in the history array
$history = (array) $_SESSION['history'];
array_unshift($history, $postId);
if (count($history) > $historyMaxUrl) {
array_pop($history);
}
$_SESSION['history'] = $history;
}
// Display latest viewed posts (or pages) wherever you want
echo '<ul>';
foreach ($_SESSION['history'] as $lastViewedId) {
echo '<li>' . get_permalink($lastViewedId) . '</li>';
}
echo '</ul>';
You can also store latest viewed custom post types (CPT) by placing the so7035465_store_last_id() function in your single-cpt.php template.
You can also add it to a hook or inject it in your template as an action, but that is beyond the scope of this question.
Hey guys, I'm using Twitter's PHP API, called twitterlibphp, and it works well, but there's one thing that I need to be able to initiate, which is the linking of URLs and #username replies. I already have the function for this written up correctly (it is called clickable_link($text);) and have tested it successfully. I am not too familiar with parts of twitterlibphp (link goes to source code), and I am not sure where to put the function clickable_link() in order to make URLs and #username's clickable. I hope that is enough information, thanks a lot.
EDIT:
In addition, I would like only one status to come up in the function GetFriendsTimeline(), right now 20 come up, is there any easy way to limit it to one?
I would extend the Twitter class and put the functionality in my own getUserTimeline method.
class MyTwitter extends Twitter
{
public function getUserTimeline()
{
$result = parent::getUserTimeline();
// Your functionality ...
return $result;
}
}
You don't need to put clickable_link() in twitterlibphp. Instead, call it right before you output a status message. Example:
$twitter = new Twitter('username', 'password');
$result = $twitter->getUserTimeline();
... parse the $result XML here ...
echo 'Status : '.clickable_link($status);