How to use safely wordpress $wpdb - wordpress

I am working on a Wordpress website. I need to display dynamic data from a table of my database and display them in one of my pages.
I used the following code (in my page) to retrieve my data:
global $wpdb;
$result = $wpdb->get_results ( "SELECT fieldA, fieldB, fieldC FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td align="center">' . $print->fieldA. '</td>';
echo '<td align="center">' . $print->fieldB. '</td>';
echo '<td align="center">' . $print->fieldC . '</td>';
echo '</tr>';
Everything is working properly and I can see the data on my page.
The problem is that if I do a right click on my page and select "View page source" on my brower, then everyone can see my query.
I think this is not very safe. How can I convert my code so no one can see my table name and fields?
Thank you in advance!

Related

Send custom field value to email

I created custom post type named Emails, and added a custom field using advanced custom fields plugin to one post inside the custom post type called email footer, the field is image field that is supposed to show at the bottom of each automatic email going out of the website.
the current code I'm using
function wpcf7ev_verify_email_address2( $wpcf7_form ){
$email_footer = '<html>
<body style="color:#000000;">
<div style="font-size:16px;font-weight:bold;margin-top:20px;">
Regards,
<br/>
$email_footer .= '<img src="http://mysite.col/footer_image.jpg" width="100%" alt=""/>
</div>';
$email_footer .='<div style="display:none;">'.generateRandomString().
'</div></body>
</html>
';
the code is working, it displays the image with this url at the bottom: http://mysite.col/footer_image.jpg
but I don't want hardcoded, I want to be able to modify it with the custom field I created
I looked at ACF documentation and found this, but I don't know how to use it to still show that exact field on custom post type I created:
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
The code you've outlined from the ACF documentation tells you how to get the image from an ACF field using Image (with type array).
If we were to implement this into your function, we'd have to reference the image from the page somewhere. Without knowing how you're calling this there are a couple of ways you could embed it.
The first way, we pass it through to the function called on the page, like so...
wpcf7ev_verify_email_address2(get_field('image'));
and then update your function like so...
function wpcf7ev_verify_email_address2($image, $wpcf7_form)
{
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
// get the image from the passed in image function.
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
Or, the second way, if you are calling the function to modify an action or something, you'd have to get the image from whichever page ID / options page it is assigned to in your ACVF settings. This would make your function look a little like this:
function wpcf7ev_verify_email_address2($wpcf7_form)
{
// get image acf field from page with id 1
$image = get_field('image', 1);
// or get image from acf field on options page
// $image = get_field('image', 'options');
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
All of the above is presuming that your function is working as intended, with you needing help grabbing the ACF field, and the image is uploaded. You can wrap your declarations of get_field in if statements if required.

Templating a Wordpress Plugin?

I have just created a simple Wordpress Plugin which basically gets some data from sources and displays it using a loop.
I want to be able to template this so that users can have a choice in how it displays or even create their own.
Something like Smarty would be fantastic but I can't seem to find a way to do this?
Currently my code is like this:
echo '<div class="row">';
foreach ($results->products as $product) {
echo '<div class="col-md-4 col-xs-12 text-center fiflitem">';
echo '<div class="thumbnail"><img src="' . $product->thumbnail . '" width="125px" height="125px"></div>';
echo '<div class="prodname">'.$product->product_name.'</div>';
echo '<div class="store"><img src="'.$product->merchant_logo.'" alt="'.$product->merchant_name.'"></div>';
if ($product->price_was!='0.00') {
echo '<div class="pricing">£'.$product->price.' (was £'.$product->price_was.')</div>'; }
else {
echo '<div class="pricing">£'.$product->price.'</div>';
}
echo '<div class="buybutton"><a href="' . $product->link . '" target="_blank" class="btn btn-default">More Info</div>';
echo '</div>';
}
echo '</div>';
So as you can see it is going to be quite complicated for somebody to edit, however if it was using something like Smarty (or similar) I could simplify the templates a lot more for users to be able to edit it.
Where would I start trying to get something like this working inside the plugin?
In the end I just installed Smarty as part of the plugin and using that function for templating the plugin.

Wordpress : display ACF checkbox label on front end

I have created chekboxes with ACF, now I can display the values ​​of the checkboxes on my front end but I would like to show the associated labels to.
Here is my code :
echo "<ul>";
$profession = get_field('profession');
foreach($profession as $key => $check){
echo "<li>".$check."</li>";
};
echo "</ul>";
Thanks a lot !
try this: http://www.advancedcustomfields.com/resources/functions/get_field_object/
You can return all the field's metadata with get_field_object().
if( get_field('field_name') ):
echo '<div class="class_for_display">';
echo 'Label:'; echo'<p>' .the_field('field_name').'</p>';
echo '</div>';
endif;
i use this code in function.php of child theme

Need help calling up Custom Fields/Values in Wordpress theme

here's the correct code: (Thanks!) (Just had to add '. .' and the value in between when echo)
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
If you still want to read the problem, here it is below.
Problem:
Currently, I have a custom field created called "ytuser" on one of my posts. Inside that field, I have typed in a youtube username (for example: youtubeuser1). What I am trying to do now is put that value at the end of a href like this: <a href="http://www.youtube.com/$value"/> so that it actually returns the following address: http://www.youtube.com/youtubeuser1 and the text then links to that user's page.
So far I have this:
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
But that only gives me a link to "http://www.youtube.com/"
I can't add $value directly to it...
Is there a way I can include the $value as part of the href? So that it can make the text link go to http://www.youtube.com/$value ?
I know this is a noob question since it can't be that hard.
I would just do $ytuser = get_post_custom_values("ytuser"); and then call the variable $ytuser[0]; where you need it like echo "<a href='http://www.youtube.com/".$ytuser[0]."'>".$ytuser[0]."</a>";. That should work!
Also, in your code example, you want to switch the " and ' usage in your echo and make sure you echo the user after the YouTube URL and as the link itself (if that's what you want).

Best way to add two row list for CMS (wordpress)

What is the best way for me to add a two row list with employees to a Wordpress CMS?
Like this: live example
| picture | personal data |
In the example above I've used a table, but this would properly work better created as CSS.
I need a way for the client to add new employees without writing ANY code. I've considered using the image caption (but I can't style it the way I want) or create a list UL - LI
I've also looked at some table plugins, but it isn't exactly working as I need. Perhaps I need to write such a plugin?
Any ideas as to how I can solve this problem?
I've solved the problem :-D
I simply have to add new user profiles as 'subscriber' and install the 'user photo' plugin. Then I can display the user data the way I want.
This is what I ended up using:
<div class="personale_table">
<?php
$wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID");
foreach ( $wp_user_search as $userid ) {
$user_id = (int) $userid->ID;
$user_login = stripslashes($userid->user_login);
$display_name = stripslashes($userid->display_name);
$user_info = get_userdata($user_id);
?>
<?php
echo('<div class="personale_thumb personale_ramme">' . get_avatar($user_id) . '</div>');
echo('<div class="personale_data"><div class="personale_linje personale_title">' . $user_info->first_name . ' ' . $user_info->last_name . '</div>');
echo('<div class="personale_linje personale_stilling">' . $user_info->aim . '</div><div class="personale_linje">' . $user_info->yim. '</div><div class="personale_linje">' . $user_info->user_email . '</div><div class="personale_linje">' . $user_info->jabber . '</div></div>');
}
?>
</div>

Resources