I've encountered a problem with a custom shortcode i created, which just outputs a table via PHP-Echo.
This shortcode just serves as a search form and posts the data to another site. Everytime i insert the shortcode in a wp-site and update it, i get "Invalid JSON-Response".
I've tried some troubleshooting and found out that:
The problem just persists with that one shortcode, another custom created shortcode does not error at all. So i guess the problem really "just" lies within the shortcodes-function.
<?php
function function_name() {
require( WP_PLUGIN_DIR.'/<dir>/assets/runtime/shortcode/function_name/form.php' );
global $wpdb;
$filter = $_POST['filter'];
$job_table_name = '<tableName>';
// Check if the Job-Table has Entries
if ( $wpdb->get_var( 'SELECT * FROM '.$job_table_name ) == null OR $filter == null ) {
exit;
}
// Run The Query With Selected Filter
$query = 'SELECT id, column1, column2, column3 FROM '.$job_table_name." WHERE column1 LIKE '%$filter%' OR id LIKE '$filter'";
// echo $query;
$results = $wpdb->get_results( $query, ARRAY_A );
//echo var_dump( $results );
// Display The Jobs From The Result Set
echo '<table>';
echo '<tr>';
echo '<th> column1</th>';
echo '<th> column2</th>';
echo '<th> column3</th>';
foreach ( $results as $result ) {
echo '<tr>';
// echo "<td><a href=.site_url().'/displayjobs?id=".$result['column1'].'>'.$result['column2'].'</a></td>';
?>
<td>
<a href = '<?php echo site_url()?>/displayjobs?id=<?php echo $result["id"]?>'><?php echo $result['column1']?></a></td>
<?php
echo '<td>'.$result['column2'].'</td>';
echo '<td>'.$result['column3'].'</td>';
}
echo '</table>';
}
?>
Here's the form i'm rendering:
<form method='POST'>
<input type='text' name='filter' placeholder='someval ...'/>
<input type='submit' value='someval ...'/>
</form>
Any ideas on why it fails?
CodePudding user response:
I think i've solved the problem myself:
- I've removed the exit-PHP-Call, which ends the script when the condition is met.
- I've added ob_start() and ob_get_clean() to the script
Since I've done that, there are no errors anymore and the plugin can be displayed just fine.
<?php
function function_name() {
global $wpdb;
$job_table_name = 'someval';
$num_rows = $wpdb->get_results( 'SELECT COUNT(*) as num_rows FROM '.$job_table_name );
$filter = $_POST['filter'];
ob_start();
require( WP_PLUGIN_DIR.'/someval/assets/runtime/shortcode/form.html' );
// return ob_get_clean();
if ( $filter == '' ) {
$filter = '*';
}
// Check if the Job-Table has Entries
// if ( $wpdb->get_var( 'SELECT * FROM '.$job_table_name ) ) {
// }
// Run The Query With Selected Filter
$query = 'SELECT id, column1, column2, column3 FROM '.$job_table_name." WHERE column2 LIKE '%$filter%' OR id LIKE '$filter'";
// echo $query;
$results = $wpdb->get_results( $query, ARRAY_A );
//echo var_dump( $results );
// Display The Jobs From The Result Set
if ( $filter != '' ) {
// echo 'FILTER: '.$filter;
echo '<table>';
echo '<tr>';
echo '<th> column1 </th>';
echo '<th> column2 </th>';
echo '<th> column3 </th>';
foreach ( $results as $result ) {
echo '<tr>';
// echo "<td><a href=.site_url().'/somelink?id=".$result['column1'].'>'.$result['column2'].'</a></td>';
?>
<td>
<a href = '<?php echo site_url()?>/displayjobs?id=<?php echo $result["coilumn1"]?>'><?php echo $result['column2']?></a></td>
<?php
echo '<td>'.$result['column3'].'</td>';
echo '<td>'.$result['column4'].'</td>';
}
}
echo '</table>';
return ob_get_clean();
}
?>
CodePudding user response:
Looking good!
So it no longer returns the "invalid JSON response"?
You should consider using $wpdb->prepare and sprintf variables for security. Illustrated below. (I'm not sure on the SQL LIKE wildcards. Codex says "All % characters inside SQL string literals, including LIKE wildcards, must be double-% escaped as %%." ... you'll have to experiment.)
Also, when using output buffer, you can simplify and eliminate some of the 'echo' commands, as illustrated below.
Other updates below:
- error handing/no results output
- if statements to run while counting through each data in each row, in case you want to do something different each time
(Aside from the updates - this is basically your script.)
<?php
function function_name() {
global $wpdb;
$tbl_name = 'someval';
$num_rows = $wpdb->get_results( $wpdb->prepare( 'SELECT COUNT(*) as num_rows FROM %s', $tbl_name ) );
$filter = $_POST['filter'];
require( WP_PLUGIN_DIR.'/someval/assets/runtime/shortcode/form.html' );
if ( $filter == '' ) { $filter = '*'; }
$rows = $wpdb->get_results(
$wpdb->prepare(
'SELECT id, column1, column2, column3 FROM %s WHERE column2 LIKE "%% %s %%" OR id LIKE "%% %s %%"',
$tbl_name,
$filter,
$filter
), ARRAY_A );
if ( $filter != '' /* && $rows !== ERRORMESSAGE */ ) {
ob_start();
?>
<table>
<tr>
<th> column1 </th>
<th> column2 </th>
<th> column3 </th>
</tr>
<?php
$c = 0;
foreach ( $rows as $row ) {
$c ; ?>
<tr>
<?php if($c==1): ?>
<a href="<?php echo site_url().'/somelink?id='.$row[0]; ?>"><?php echo $row[1]; ?></a>
<?php elseif($c==2): ?>
<span><?php echo $row[2]; ?></span>
<?php else: ?>
<span><?php echo $row[3]; ?></span>
<?php endif; ?>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<p>Ah, shucks. </p>
<?php }
return ob_get_clean();
}
?>