trying to display a custom field - wordpress

Guys I'm writting a wordpress site to run a knowldge base for our Service desk. As one person will be updating it i needed to have a field for who wrote the kb artical. I'm tring to add a custom field into my wordpress theme to display writtenby using Advance custom Fields. Now I'm using echo Knowledge Base plugin for knowldge base.
I've got as far add ing code below will display the text below the last up date value that plugin creates. However i cannot get it to put value from the custom field on the page after this. The plugin creates the page using php below the ive added the two lines as below.
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Writtenby:'.$wb.' ';
// LAST UPDATED ON
public static function last_updated_on( $args ) {
echo '' . esc_html( $args['config']['last_udpated_on_text'] ) . ' ' . EPKB_Utilities::get_formatted_datetime_string( $args['article']->post_modified, 'F d, Y' ).'';
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Written by:'.$wb.' ';
}

Advanced Custom Fields plugin use a little bit different system to store postmeta. Try to use get_field() instead get_post_meta()

If you have the ID
$customField = get_post_meta($my_id, "_mcf_customField", true);
However if you want to get the ID from the object:
$customField = get_post_meta($post_id->ID, "_mcf_customField", true);

After much more work looks like at the point the page was being created it had no reference to partical page not even a current one. They where writting to an array all artical numbers and info.
So by telling get_field which artical to get the writtenby field from it now displayed data and not blank
$wb= get_field('Writtenby', $args['article']->ID);

Related

Id changing variable

I'm working on a project where I upload a file and use its path in a shortcode. Right now I've hard-coded the post's ID into my code but I want to make it dynamic so that new posts automatically get the correct shortcode.
<?php
global $wpdb;
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = 5574" ) );
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $thepost->meta_value . '"]');
?>
Going off of what #Damocles said, you are really setting yourself up for trouble with your current path. Instead, there are built-in WordPress functions that abstract away the database and utilize caching that you are strongly encouraged to use.
// Get the current WordPress post
$the_post = get_post();
//If we have a value (there are cases where this will be empty)
if($the_post){
// Get the value from the post meta table by the current post's ID
$the_post_meta = get_post_meta($the_post->ID, 'YOUR_META_KEY_HERE', true);
// Double-check that we have a value
if($the_post_meta) {
// Finally, echo the shortcode
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $the_post_meta . '"]');
}
}
There are several ways to get the current post however get_post() is the most common. If you are in a custom loop, however, you might need to adjust accordingly.
To access the meta, use get_post_meta() which includes some optimizations including use a cache instead of the database.
Although there are definitely exceptions, generally speaking, if you are working with WordPress and you find yourself writing SQL statements, there is almost always a better, safer, faster, etc. way to do it using core functions.

Gravity forms: Authorize.net Invoice number

I was wondering If you knew how to add an invoice code to the forms in Authorize.net.
I check the authorize.net feed settings but they do not ask for an invoice code.Then, I started to do some research and found the hook gform_authorizenet_save_entry_id that could be used to create that invoice code.
The problem comes that there is no documentation about this hook. It was only mentioned as one of the updates. So, I'm creating a hidden field with an {entry_id} as a default value and trying to find a way to pass it as an Invoice Number.
Any help would be appreciated. Thanks :)
Update:
I was able to add a transaction code to the form using the following Snippet
//Adding the transaction code
add_filter( 'gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 5 );
function set_invoice_number( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 6 ) {
// your submission ID format to be inserted into the hidden field
$SubmissionID = 'RW-' . $entry['id'];
$transaction->invoice_num = $SubmissionID;
}
return $transaction;
}
I got the invoice number to become "RW-" but the $entry['id'] is not printing anything
You can assign an invoice number using an input field by adding this code to your theme's functions.php file:
add_filter('gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 4);
function set_invoice_number($transaction, $form_data, $config, $form)
{
$transaction->invoice_num = rgpost('input_YOUR INPUT FIELD NUMBER HERE');
}
If the input field is the hidden field containing the form's entry id that should accomplish what you're wanting.
I use the following code to append the "Form Name" to the invoice number passed to Authorize.net. I believe that the previous answer would work if the first field you create in each form was the "Invoice Number" field, but if you add the field later, or add it to your previously created forms, they wouldn't all have the same field number so it may not work. This code will use the automatically generated unique invoice number and add the form name.
i.e. Form Name: "Yearly Subscription" Auto Invioce Number: "1234567890"
Info that Authorize.net receives: "1234567890-Yearly_Subscription"
I have found that instead of adding custom functions to your theme file, it is better to build a custom plugin and add them there instead. This way if your theme updates, you won't lose your functions. The code to create your plugin, along with the functions for your Authorize.net code is included below. Save this file as My_Custom_Functions_Plugin.php and upload it to your web host in the "wp-content/plugins/" folder and then activate it. Next time you need to add any customized functions, just add them at the end of this file.
<?php
/*
Plugin Name: My_Custom_Functions_Plugin
Plugin URL: http://WEBSITE
Description: Custom Functions and Scripts for WEBSITE
Version: 1.0.0
Author: NAME
Author URI: http://WEBSITE
*/
// Functions for apending form name to authorize.net invoice
add_filter( 'gform_authorizenet_transaction_pre_capture', 'invoice_num', 10, 4 );
function invoice_num( $transaction, $form_data, $config, $form ) {
$transaction->invoice_num = uniqid() . '-' . rgar( $form_data, 'form_title' );
gf_authorizenet()->log_debug( 'gform_authorizenet_transaction_pre_capture: ' . print_r( $transaction, 1 ) );
return $transaction;}
// End of Authorize.net Function
// Add any additional Functions below this comment line to keep your themes functions.php file from getting overwritten on theme updates. Copy and paste this comment line before each function and give it a description to help keep you organized about what your function does
?>
Hope this helps!

how can we get details from another database inside a plugin in wordpress

I created a plugin in wordpress to display earnings by using a shortcode.The details to display while using shortcode are stored in another database.I used direct database connection in plugin to fetch details from the that database.I used the following code
function earnings_shortcode($atts, $content, $tag)
{ //echo $atts[0];echo '<br>';
$str=base64_encode(1);
base64_decode($str);
$length = 4;
$res = trim(preg_replace("/[^0-9]/", "", $atts[0]));
$mydb1 = new wpdb('root','','db_test','localhost');
$rows = $mydb1->get_row("SELECT total,paydate FROM `tbl_shotcode` WHERE userid = $res", ARRAY_A);
echo "Payout on -" .$rows['paydate']; echo '<br/>';
echo "Total for next Pay Period:-" .$rows['total'];
}
Is there any better option to access another database inside a plugin with out hard coding the username and password.please suggest a solution.
No, you have to access the database and be authenticated to do queries. You can't get into a properly configured database without login in.
You can, however, make your $mydb1 variable global and define it at the top of your file (or in your constructor class) to be accessed by all your functions. I recommend putting it in a class to manage it more easily.

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources