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.
Related
First, thanks for the time. I have been messing with this quite a bit and cannot figure out what is going wrong (and this is something I thought I understood) I am currently trying to change save location of files uploaded via WP. As the title suggests. The $_GET is not pulling anything from -
/?action=type1&project=1&method=add
I can replace the gets with appropriate numbers and get correct project output so I am almost certain the $_GET that isn't pulling the info. As I understand this is a supervariable so should have no problems getting into a function.
Can anyone let me know if they see any problem in my logic, syntax or am I missing something going on in upload_dir/wp_handle_upload_prefilter hook? Or are there any suggestions on troubleshooting/solving?
$_GET is used everywhere on the site and works fine otherwise.
function ca_doc_pre_upload($file){
add_filter('upload_dir', 'ca_doc_custom_upload_dir');
return $file;}
function ca_doc_custom_upload_dir($path){
if (isset($_GET['project'])) {
$projectID = $_GET['project'];
};
if (isset($_GET['action'])) {
$type = $_GET['action'];
};
$project = ca_get_project($projectID);
$customdir = '/'.strtolower(str_replace(" ", "-", $project->project_name)).'/'.$type;
$path['path'] = str_replace($path['subdir'], '', $path['path']);
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;}
add_filter('wp_handle_upload_prefilter', 'ca_doc_pre_upload', 2);
To Confirm not able to retrieve with get. Used.
if (isset($_GET['action'])) {
$type = $_GET['action'];
}else{
$type = 'Not Getting';};
And tried to save a file. It saved it to Not getting the folder. (took out the project part).
Some additional information. Created another function in function.php using get and called it on the same page where plupload happens and it worked. The only thing I can figure now it that this isn't working because its called from upload or the hook is doing something... I am only guessing now because I ran out of viable logic a long time ago.
Thanks again for the time.
Have you registered the query params with WP query_vars()? Perhaps the permalink rewriting is interfering..?
https://wordpress.stackexchange.com/a/41373
You may also need to re-save your permalink settings after you add the parameter.
With some other help and research turns out that plupload that handles the upload in Wordpress was doing some AJAX work in the background calling;
url: '<?php echo admin_url('admin-ajax.php')?>'
And for some reason the $_GETs were not going with that, so just changed the URL to include them as follows, and all was right again.
url: '<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'') ?>'
Apparently when using the hooks above this is the point(admin-ajax.php) in which it grabs the $_GETS
have you tried
get_query_var('action');
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!
URL : http://www.sayuri.co.jp/used-cars
Example : http://www.sayuri.co.jp/used-cars/B37753-Toyota-Wish-japanese-used-cars
Hey guys , need some help with one of my personal projects , I've already wrote the code to fetch data from each single car url (example) and post on my site
Now i need to go through the main url : sayuri.co.jp/used-cars , and :
1) Make an array / list / nodes of all the urls for all the single cars in it , then run my internal code for each one to fetch data , then move on to the next one
I already have the code to save each url into a log file when completed (don't think it will be necessary if it goes link by link without starting from the top but will ensure no repetition.
2) When all links are done for the page , it should move to the next page and do the same thing until the end ( there are 5-6 pages max )
I've been stuck on this part since last night and would really appreciate any help . Thanks
My code to get data from the main url :
$content = file_get_contents('http://www.sayuri.co.jp/used-cars/');
// echo $content;
and
$dom = new DOMDocument;
$dom->loadHTML($content);
//echo $dom;
I'm guessing you already know this since you say you've gotten data from the car entries themselves, but a good point to start is by dissecting the page's DOM and seeing if there are any elements you can use to jump around quickly. Most browsers have page inspection tools to help with this.
In this case, <div id="content"> serves nicely. You'll note it contains a collection of tables with the required links and a <div> that contains the text telling us how many pages there are.
Disclaimer, but it's been years since I've done PHP and I have not tested this, so it is probably neither correct or optimal, but it should get you started. You'll need to tie the functions together (what's the fun in me doing it?) to achieve what you want, but these should grab the data required.
You'll be working with the DOM on each page, so a convenience to grab the DOMDocument:
function get_page_document($index) {
$content = file_get_contents("http://www.sayuri.co.jp/used-cars/page:{$index}");
$document = new DOMDocument;
$document->loadHTML($content);
return $document;
}
You need to know how many pages there are in total in order to iterate over them, so grab it:
function get_page_count($document) {
$content = $document->getElementById('content');
$count_div = $content->childNodes->item($content->childNodes->length - 4);
$count_text = $count_div->firstChild->textContent;
if (preg_match('/Page \d+ of (\d+)/', $count_text, $matches) === 1) {
return $matches[1];
}
return -1;
}
It's a bit ugly, but the links are available inside each <table> in the contents container. Rip 'em out and push them in an array. If you use the link itself as the key, there is no concern for duplicates as they'll just rewrite over the same key-value.
function get_page_links($document) {
$content = $document->getElementById('content');
$tables = $content->getElementsByTagName('table');
$links = array();
foreach ($tables as $table) {
if ($table->getAttribute('class') === 'itemlist-table') {
// table > tbody > tr > td > a
$link = $table->firstChild->firstChild->firstChild->firstChild->getAttribute('href');
// No duplicates because they just overwrite the same entry.
$links[$link] = "http://www.sayuri.co.jp{$link}";
}
}
return $links;
}
Perhaps also obvious, but these will break if this site changes their formatting. You'd be better off asking if they have a REST API or some such available for long term use, though I'm guessing you don't care as much if it's just a personal project for tinkering.
Hope it helps prod you in the right direction.
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.
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.