Plugin not displaying function on page where shortcode is present - wordpress

This is a plugin I'm currently developing, it's function is to display a CSV file stored in the plugin directory, as an HTML table; The reason I'm writing this manually rather than using one of the many already existing plugins which perform a similar task is; I need to be able to use the html output for another script's functions that will be executed later, rather than just displaying this data.
The shortcode is functioning because it isn't visible on the page, however nothing else appears on the page.
As far as I know the csv file I'm using to test this plugin isn't corrupted, can be opened.
This is the content of Book1.csv:
test1,test2,test3
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
13,14,15
<?php
<table>
<thead>
<th>
Image
</th>
<th>
Name
</th>
<th>
Price
</th>
</thead>
<tbody>
<?php
add_shortcode( "csv", "open_csv_file");
function open_csv_file() {
$handle = fopen("Book1.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE):
}
?>
<?php
<tr>
<td><?= $data[1] ?></td>
<td><?= $data[2] ?></td>
<td>$<?= number_format($data[7], 2) ?></td>
</tr>
endwhile;
}
</tbody>
</table>
?>

I found some errors in your code, first you are not getting any output because
} for function is closed in wrong place
short tags <?= may not work if it is disabled in configuration
The following may work,
<?php
add_shortcode( "csv", "open_csv_file");
function open_csv_file() {
?>
<table>
<thead>
<th>
Image
</th>
<th>
Name
</th>
<th>
Price
</th>
</thead>
<tbody>
<?php
$handle = fopen("Book1.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE):
?>
<tr>
<td><?php echo $data[1]; ?></td>
<td><?php echo $data[2]; ?></td>
<td>$<?php echo number_format($data[7], 2); ?></td>
</tr>
<?php
endwhile;
?>
</tbody>
</table>
<?php } ?>

Related

Wordpress add user number field in contact information part

I am wondering how to achieve the following.
I try to create a number field input in the wordpress user profile under contact information.
So far if i use show_user_profile & edit_user_profile hooks, the field will appear on the bottom of the profile page.
Is it possible to create a number field under the contact information part (standard in wordpress?)
I tried to add different priorities to the actions, but that didnt solve it.
So far i have the following:
function addurenuser(){
$userid = get_current_user_id();
if ( current_user_can('update_plugins',$userid)){ ?>
<table class="form-table">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr( get_the_author_meta( 'uren', $user->ID ) ); ?>">
</td>
</tr>
</table> <?php
}
}
add_action( 'show_user_profile', 'addurenuser' );
add_action( 'edit_user_profile', 'addurenuser' );
Use this code
function addurenuser()
{
echo '<script>
jQuery(document).ready(function($){
$(".your-custom-field").insertAfter($(".user-email-wrap").closest("table"));
});
</script>';
$userid = get_current_user_id();
if (current_user_can('update_plugins', $userid)) { ?>
<table class="form-table your-custom-field">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr(get_the_author_meta('uren', $userid->ID)); ?>">
</td>
</tr>
</table> <?php
}
}
add_action('show_user_profile', 'addurenuser');
add_action('edit_user_profile', 'addurenuser');

Editing the invoice template in Woocommerce

I need to edit the Woocommerce invoice template with coding. I Copied all the files from wp-content/plugins/woocommerce-pdf-invoices-packing-slips/templates/Simple to to my child them in wp-content/themes/church-event-child/woocommerce/pdf/yourtemplate and customized them there. I need the template like this:
Subtotal: € 0
IVA: € 0
Order total: € 0
But woocommerce simple template only shows it like this
Subtotal: € 0
Order total: € 0
So it is about adding the IVA with the amount inside. Here you can see the code I am trying to change, but I dont know how to find where the label of the tax_amount is kept and how to display it right.
<table class="totals">
<tfoot>
<?php foreach( $this->get_woocommerce_totals() as $key => $total ) : ?>
<tr class="<?php echo $key; ?>">
<td class="no-borders"></td>
<th class="description"><?php echo $total['label']; ?></th>
<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
</tr>
<?php endforeach; ?>
</tfoot>
</table>
Thanks in Advance

get_the_terms not working in custom plugin

I have created custom plugin which stores the user data in db table. The form have dropdown which have taxonomy id as value.
The form is working and taxonomy id and other user data is inserting in the database.
In the wp admin panel i am showing the data that has been inserted in the database. All the data is coming with the taxonomy id. But when i use get_the_terms to get the name according to the taxonomy id it shows nothing.
Here is the code:
global $wpdb;
$table_name=$wpdb->prefix .'opu_userdata';
$data=$wpdb->get_results("SELECT * FROM $table_name order by id DESC");
<table class="table table-bordered" id="tow-table">
<thead>
<tr>
<th>Sno</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Product</th>
<th>Product Sub</th>
<th>Annual Salary</th>
<th>Employee Type</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
$sn=1;
foreach($data as $data):?>
<tr>
<td><?php echo $sn;?></td>
<td><?php echo ucwords($data->name);?></td>
<td><?php echo $data->email;?></td>
<td><?php echo $data->mobile;?></td>
<td><?php echo $data->product;?></td>
<td><?php
if($data->product=="bank-account"){
$id=$data->product_sub;
$n=get_the_terms($id,'account_type');
echo $id; /////if i echo id it prints the id perfectly but when i try $n[0]->name it shows nothing (var_dump($n) shows bool(false) )
}?></td>
<td><?php echo $data->annual_salary;?></td>
<td><?php echo $data->emp_type;?></td>
<td><?php echo $data->age;?></td>
</tr>
<?php $sn++; endforeach;?>
</tbody>
Ok i got the solution. I have to use get_term_by instead of get_the_terms to get the term name according to the taxonomy id
Here is the solution:
<table class="table table-bordered" id="tow-table">
<thead>
<tr>
<th>Sno</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Product</th>
<th>Product Sub</th>
<th>Annual Salary</th>
<th>Employee Type</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
$sn=1;
foreach($data as $d):?>
<tr>
<td><?php echo $sn;?></td>
<td><?php echo ucwords($d->name);?></td>
<td><?php echo $d->email;?></td>
<td><?php echo $d->mobile;?></td>
<td><?php echo $d->product;?></td>
<td><?php
if($d->product=="bank-account"){
$id=$d->product_sub;
$term=get_term_by('id',$id,'account_type');
echo $term->name;
}?></td>
<td><?php echo $d->annual_salary;?></td>
<td><?php echo $d->emp_type;?></td>
<td><?php echo $d->age;?></td>
</tr>
<?php $sn++; endforeach;?>
</tbody>

Add custom cart field to woocommerce

This may be a noob question, but I've googled a lot and haven't found the answer. I've already replaced the woocommerce cart to my child theme subfolder.
I'm trying to add a field to the woocommerce cart, so it will just display the cart subtotal multiplied by a number defined by me. I.e., the cart subtotal is 3,000.00, I want to define the multiplier number 3, so the field I want to show must display 9.000,00.
I've already added the field header to the cart:
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-quantity"><?php _e( 'Quantity', 'thefoxwp' ); ?></th>
<th class="product-subtotal"><?php _e( 'Total', 'thefoxwp' ); ?></th>
<th class="product-result"><?php _e( 'Total to be Received', 'thefoxwp' ); ?></th>
</tr>
</thead>
The field I want to configure is the class="product-result".
It's not a input field, it will just show the subtotal multiplied by a number defined by me.
I need this information to be sent with the woocommerce email to the client and saved in my order details.
Does anybody know how to do it?
Thanks
Could figure it out.
I added two classes to the header
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-quantity"><?php _e( 'Quantity', 'thefoxwp' ); ?></th>
<th class="product-subtotal"><?php _e( 'Total', 'thefoxwp' ); ?></th>
<th class="product-tax"><?php _e( 'Exchange Tax', 'thefoxwp' ); ?></th>
<th class="product-result"><?php _e( 'Total to be Received', 'thefoxwp' ); ?></th>
</tr>
</thead>
And then added this code
<!-- Exchange Tax -->
<td class="product-tax">
<?php
$tax = 4.05;
echo "R$ $tax";
?>
</td>
<!-- Product Result -->
<td class="product-result">
<?php
$resultado = WC()->cart->subtotal;
$mostrar = $resultado * $tax;
echo "R$ "; echo number_format("$mostrar",2);
?>
</td>
So it's now echo the result of the cart subtotal * the exchange tax.
But now, how can I get this variable to echo on the email sent by woocommerce and on the checkout page?

$wpdb not work in another page

i create simple plugin , it work in main page , but when i create a link to user go to the another page for see informaation , it show me Fatal error: Uncaught Error: Call to a member function get_results()
my code is :
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM wp_customers");
?>
<body>
<table>
<tr>
<th>‌ID</th>
<th>Name</th>
<th>Family</th>
<th>Numbers</th>
<th>Tell</th>
</tr>
<?php foreach($results as $results){
?>
<tr>
<td>
<?php echo $results->id; ?>
</td>
<td>
<?php echo $results->name; ?>
</td>
<td>
<?php echo $results->family; ?>
</td>
<td>
<?php echo $results->numbers; ?>
</td>
<td>
<?php echo $results->tell; ?>
</td>
<?php }?>
</tr>
</table>
You need to check you have not set up the database prefix to something other than 'wp_'.
And if you have wp_ prefix in the database then you have to included wp-load file on the custom page.
require( '/path/to/wp-load.php' );
If it's a plugin then you should encapsulate your code in a function and hook this funtion to the wordpress "init" action, so in your plugin file you should have somthing like this :
function your_function_name_here($some_params){
return "some results";
}
add_action('init', 'your_function_name_here');

Resources