Wordpress not pulling Dircaster script on demand - wordpress

I have a script which when running standalone as a php works fine. It's a simple output of an rss feed but the rss is generated on the fly by dircaster. When I turn this into a wordpress plugin, however, it fails to work every time.
This is the error which is often given. Pressing refresh sometimes then makes it work.
Warning: array_slice() expects parameter 1 to be array, null given in /home/evilmer/public_html/frome.me/ffm/wp-content/plugins/fromefm-player/fromefm-player-plugin.php on line 50
Warning: Invalid argument supplied for foreach() in /home/evilmer/public_html/frome.me/ffm/wp-content/plugins/fromefm-player/fromefm-player-plugin.php on line 53
This is the code which is generated based on [ffmplayer show=xxx showno=xx]. I have not included the entire shortcode code as I don't think it's neccesary.
include_once(ABSPATH . WPINC . '/rss.php');
$num_items = $showno;
$feedurl = 'http://fromefm.co.uk/archive/dircasterX.php?show='.$show;
$feed = fetch_rss($feedurl);
$items = array_slice ($feed->items, 0, $num_items);
$list = "";
foreach ($items as $item )
{
$title = $item[title];
$mp3link = $item[link];
$description = $item[description];
$list .= "$title - $description</br><audio src='$mp3link' preload='none'> </audio>";}
return "
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
$list
";
Line 50 is:
$items = array_slice ($feed->items, 0, $num_items);
And line 53 is
foreach ($items as $item )
I'm convinced that it's just not running the DircasterX.php (dircaster.org) script properly or every time but it seems to work ok when I use it standalone and calling it with magpierss instead of the version (rss.php) which is built into wordpress.
The standalone version is currently here http://www.fromefm.co.uk/popupplayer/five.php?show=homelyremedies&showno=6 Instead of using wordpress shortcode it gets the variables from $_get instead.
There is a demo install of the plugin here (please ignore the js error on fromfmplayer.js as it's unrelated) http://frome.me/ffm/?page_id=48

The warning you got tells you that $feed is null. Probably because fetch_rss($feedurl) didn't its results successfully.

Related

Wordpress function cannot call submit_button() as it ends with "undefinied function"

i am a real very newbie in coding and in Wordpress. Trying my first test plugin to understand basics. I am able to define plugin, register it, so I can see it in plugins, activate it.
My later goal is to be able to create custom form, save user-specific data to new DB table, and then enable reading/editing it.
I tried to follow the instructions from gmazzap placed here: https://wordpress.stackexchange.com/questions/113936/simple-form-that-saves-to-database
I just am having following error from WP in time of trying to display preview of new screen having a shortcode [userform] in it:
*Fatal error: Uncaught Error: Call to undefined function submit_button() in /data/web/virtuals/131178/virtual/www/subdom/test/system/wp-content/themes/twentytwentythree-child/functions.php:14
*
My functions.php of my theme now looks like this:
<?php
add_action('init', function() {
add_shortcode('userform', 'print_user_form');
});
function print_user_form() {
echo '<form method="POST">';
wp_nonce_field('user_info', 'user_info_nonce', true, true);
?>
All your form inputs (name, email, phone) goes here.
<?php
submit_button('Send Data');
echo '</form>';
}
add_action('template_redirect', function() {
if ( ( is_single() || is_page() ) &&
isset($_POST['user_info_nonce']) &&
wp_verify_nonce($_POST['user_info_nonce'], 'user_info')
) {
// you should do the validation before save data in db.
// I will not write the validation function, is out of scope of this answer
$pass_validation = validate_user_data($_POST);
if ( $pass_validation ) {
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
);
global $wpdb;
// if you have followed my suggestion to name your table using wordpress prefix
$table_name = $wpdb->prefix . 'my_custom_table';
// next line will insert the data
$wpdb->insert($table_name, $data, '%s');
// if you want to retrieve the ID value for the just inserted row use
$rowid = $wpdb->insert_id;
// after we insert we have to redirect user
// I sugest you to cretae another page and title it "Thank You"
// if you do so:
$redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
// previous line if page titled 'Thank You' is not found set the current page
// as the redirection page. Next line get the url of redirect page:
$redirect_url = get_permalink( $redirect_page );
// now redirect
wp_safe_redirect( $redirect_url );
// and stop php
exit();
}
}
});
Note: I have not got to DB exercise, point of my question is submit_button.
As indicated in the error, the code on line 1 points to non identified function:
submit_button('Send Data');
I understood from other discussions submit_button should be core function of WP, so I should be able to call it "directly", without the need of a definition.
I tried following:
originally had very similar code within the plugin, moved to functions.php
reinstalled core of WordPress version 6.1.1
tried several different Themes (as it looked this worked for other users, I tried "classic" and "twentytwentythree" )
And still no little step further, still having same issue with error described above. What I am doing wrong?
If someone would confirm this is WP core installation issue, I am ready to reinstall WP from scratch, just trying to save some time, if there might be other cause.
Thank you for any suggestions.

Custom Taxonomies and how to stay in them when the posts have multiple terms selected?

Wondering if anyone can help me think this one out?
Whilst in lock-down, I've been putting together a simple site to organise my collection of Retro Gaming Adverts covering systems from the Atari 2600 up to the N64. I've still got a few 1000 to add to the site (takes time) but i've come across an issue I'm not sure how to implement a fix for.
You can browse the adverts by system through their single post pages but if an advert covers multiple systems it messes up the previous and next posts link and will drop you into another system.
For Example: If you're using the next and previous post links to go through the "mega drive / genesis" section once you get to " Battletoads and Double Dragon " when you press the next post arrow this time you're suddenly going through adverts tagged as NES, due to the fact thats the first term associated with it.
See : https://www.retrogameads.com/system/mega-drive/ and click on the first advert, then keep pressing the next arrow and you'll see what i mean.
I guess I could post each advert multiple times for each system but I don't like the idea of that.
Anyone got any suggestions on how I could work out what Term the user was browsing and keep them in that one?
Bare in mind this site is a work in progress so the design is just something basic till i work out the best way to organise things.
Let me know your thoughts.
Update:
Current method for getting prev next posts...
<?php
$terms = get_the_terms( $post->ID, 'system' );
$i = 0;
$systems = array();
foreach ( $terms as $term ) {
$systems[$i] = $term->slug;
$i++;
}
$postlist_args = array(
'posts_per_page' => -1,
'post_type' => 'portfolio',
'system' => $systems[0],
'order' => 'ASC',
'orderby' => 'title'
);
$postlist = get_posts( $postlist_args );
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
?>
<div class="prev_next">
<?php
if ( !empty($previd) ) {
echo '<div class="older"><a rel="prev" href="' . get_permalink($previd). '">‹</a></div>';
}
if ( !empty($nextid) ) {
echo '<div class="newer"><a rel="next" href="' . get_permalink($nextid). '">›</a></div>';
}
?>
When you click on an advert, you're essentially visiting the single page for that advert. At that moment, WP does not know/remember that the user only wants to see adverts from the system you clicked.
How are you rendering the previous/next links right now?
One possible solution could be to add a parameter to the URL and then take this into account in the PHP code when rendering the previous/next links. (/portfolio/advert-name/?system=mega-drive for example)
Edit: Of course the URL could also be made prettier... if you want to put in some extra work, you could register a permalink of the form /portfolio/mega-drive/advert-name/ for example. But this would require a bit more work.
Would that work? I think it would be the best solution considering the alternatives.
Update: For the actual implementation, the get_next_post_where and get_previous_post_where filters might prove to be very useful. You could make it so that it takes the system parameter into account for the previous/next links.
Another option: You could also set up a PHP session and remember the current system that way, but then you will require a PHP session, which is not a good thing, and it will also prevent you from using full page caching (performance optimization).
Yet another option would be to remember the current system client-side through a cookie. But in that case you have caching issues again unless you load the previous/next links through AJAX.
After reading the update on your question, I have the following notes:
system is not a valid argument and will be ignored on the get_posts() call.
the method you use to get the previous and next posts is very inefficient, because you query the whole database, and then you save everything in memory, and then you comb through the whole result set in memory, using a lot of unnecessary ram and CPU power
So how can we improve this?
Simple: Use the get_previous_post and get_next_post functions on the $post. It accepts three arguments. Here is the example for get_next_post (but get_previous_post is basically the same):
get_next_post( bool $in_same_term = false, array|string $excluded_terms = '', string $taxonomy = 'category' )
Now if you set $in_same_term to true then the next post will be of a post that shares at least one taxonomy term (system in our case).
You also have to set $taxonomy to system because that is the custom taxonomy.
And the other parameter is $excluded_terms. Too bad there is no $included_terms otherwise we could just put the system we want in there. But we can do it another way... We can filter out the current system (in the system GET parameter in our URL) and keep all other systems as systems to exclude.
So let's build what we need now.
global $post;
// What system did we come from?
$system = $_GET['system'] ?? null; // Set to null if nothing was specified (uses PHP's null coalescing operator available since PHP 7
// Exclude nothing by default
$excluded_term_ids = [];
// If we came here by clicking on a system, then exclude all other systems so the previous/next links will be of the same system only
if ($system) {
// Retrieve system terms for this post
$terms = get_the_terms( $post, 'system' );
// Filter the list of terms to remove the system we came from
// This way we can create a list of terms to exclude
$excluded_terms = array_filter( $terms, function ( $term ) use ( $system ) {
return $term->slug != $system;
} );
// The get_previous_post and get_next_post functions expect $excluded_terms to be an array of term IDs, so we must map the WP_Term objects into IDs
$excluded_term_ids = array_map( function ( $term ) {
return $term->term_id;
}, $excluded_terms );
}
// Retrieve previous and next post
$previous_post = $post->get_previous_post( true, $excluded_term_ids, 'system' );
$previous_post = $post->get_next_post( true, $excluded_term_ids, 'system' );
// Echo out the page links
// And don't forget to re-add the ?system= parameter to the URL
$url_suffix = $system ? ('?system=' . $system) : '';
if ( $previous_post ) {
echo '<div class="older"><a rel="prev" href="' . get_permalink($previous_post) . $url_suffix . '">‹</a></div>';
}
if ( $next_post ) {
echo '<div class="newer"><a rel="next" href="' . get_permalink($next_post) . $url_suffix . '">›</a></div>';
}
NOTE: Untested code.. So there might be a small bug somewhere.. Let me know if you encounter an issue with this code..
Important: On the page of a system, you must now also add '?system=current-system' to the URLs that you render.
Probably something like: echo get_permalink($advert) . '?system=' . $post->post_name (could be different depending on how your code is on that screen..)

Wordpress shortcode only returning 1 from feed despite using foreach

I am trying to create a custom Wordpress plugin which utilises shortcodes to output what I want. In this text code I am attempting to read an rss file and spit out just a list of the top 5 feeds.
The $showno is one of the shortcode variables so I can use the following [player show=foo snowno=5]. In the example code $show isn't used.
The code below only shows the most recent item in the feed list. If I change the return to echo then it works as expected except it show at the top of the post instead of where I've entered the shortcode. I searched for an answer to this and the solution offered was simply "use return" which I've done...
Appreciate advice. Thanks
include_once(ABSPATH . WPINC . '/rss.php');
$num_items = $showno;
$feedurl = 'http://feeds.bbci.co.uk/news/rss.xml';
$feed = fetch_rss($feedurl);
$items = array_slice ($feed->items, 0, $num_items);
foreach ($items as $item ) {
$title = $item[title];
$mp3link = $item[link];
$description = $item[description];
return "<li>$title - $description</li>";
}
Shortcodes should be returned not echoed.
In your code, you are breaking the execution of the foreach and returning the first value.
You should build a string variable and after the foreach loop return it, so all your loop will be included, e.g.:
$final_html = '';
foreach( $items as $item )
{
$final_html .= "<li>$title - $description</li>";
}
return $final_html;

simple_html_dom: Call to a member function find() on a non-object in

This code has been working good for months. The output consisted of a few lines containing statistics for a given username. The website from which the html page is taken is not down, and the content of the page in file_get_html hasn't changed.
All of a sudden (I checked and nobody modified it) it stopped working. Here's the relevant part:
[...]if ($FileAge > ($expiretime * 60) || 0 == filesize($cachename))
{
include_once('simple_html_dom.php');
$html = file_get_html('http://www.foo.com/search?what=user%3A'.YOUR_USER.'&search=Search');
var_dump($html); //TEST
$link = $html->find('.likeh4 lightGrey.', 0)->find('a', 0)->href; // Get the last activity link
[...]
The error log says:
[02-Feb-2013 17:02:19 Europe/Berlin] PHP Fatal error: Call to a member function find() on a non-object in /foo.php on line 22 (the line with $link).
var_dump($html) gives bool(false)
I have a similar script which parses an html page from another website. It stopped working as well.
[...]include_once('simple_html_dom.php');
$html = file_get_html('http://my.flightmemory.com/'.FLIGHTMEMORY_USER);
$chilometri_table = $html->find('table', 2); [...]
I tried to save on my webserver one of those html pages and I don't get such error.
Did my host disable some php function for security reasons? (actually, file_get_html comes from simple_html_dom and not from php native functions)
Any hints?
Thanks
It is probably way too late but:
Simple_html_dom has a constant to check given html size - MAX_FILE_SIZE. by default it's 600KB. It's enough for most cases, but if your given html is bigger than that, it will fail and returns false and causes that fatal error.
If you try to get [href]
I had same problem and fixed it.
need valid it's a simple_html_dom_node
if(is_a($html->find('.likeh4 lightGrey. a', 0),'simple_html_dom_node' )
$link = $html->find('.likeh4 lightGrey. a', 0)->href;
OR
foreach ( $$html->find('.likeh4 lightGrey. a') as $links ) {
$link =$links->href;
}

Getting deprecated error with Simplepie

I have installed the latest Simplepie code (1.2.1) and I am using the demo code they provide:
<?php
require 'simplepie.inc';
$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();
// default starting item
$start = 0;
// default number of items to display. 0 = all
$length = 0;
// if single item, set start to item number and length to 1
if(isset($_GET['item']))
{
$start = $_GET['item'];
$length = 1;
}
// set item link to script uri
$link = $_SERVER['REQUEST_URI'];
// loop through items
foreach($feed->get_items($start,$length) as $key=>$item)
{
// set query string to item number
$queryString = '?item=' . $key;
// if we're displaying a single item, set item link to itself and set query string to nothing
if(isset($_GET['item']))
{
$link = $item->get_link();
$queryString = '';
}
// display item title and date
echo '' . $item->get_title() . '';
echo ' <small>'.$item->get_date().'</small><br>';
// if single item, display content
if(isset($_GET['item']))
{
echo ' <small>'.$item->get_content().'</small><br>';
}
echo '<br>';
}
?>
However, when I load the page in my browser, I get dozens of lines saying:
Deprecated: Assigning the return value of new by reference is deprecated in /home/pliggs/public_html/rss/simplepie.inc on line 7722
Anyone know what's wrong?
I ran their compatibility test and it shows all things have passed.
This is a result of SimplePie's PHP 4 compatibility and is nothing in your code. If you want to stop seeing these errors, exclude E_DEPRECATED from your error_reporting:
error_reporting(E_ALL & ~E_DEPRECATED);
If you'd like to fix the errors themselves, you can grab a copy of SimplePie 1.3-dev (which drops PHP 4 compatibility) from GitHub, although keep in mind this is a development version and is unstable.
You need to find every instance of "=& new" in the code and delete the "&" which is now deprecated. There are approximately 116 occurrences in the code. It has to do with copies and references of object instantiation.
The only occurrence of error_reporting I could find in Version 1.2.1 was this line:
if ((ini_get('error_reporting') & $level) > 0)
This was in simplepie.inc
I'm still not sure how to disable all these warnings short of going with the dev version which I'd prefer not to as I have enough code to debug as is.

Resources