How to update meta value to exactly what it appears at frontend? - php

I tried updating a meta value from wordpress frontend using the following code:
function code_form()
{
$content = '';
$content .= '<h2> code </h2>';
$content .='<form method="post">';
$content .='<input type = "text" name="code"/>';
$content .= '<br /><input type="submit" name="Activate" value="continue">';
$content .= '</form>';
return $content;
}
add_shortcode('form1','code_form');
function a()
{
if(isset($_POST['Activate']))
{
$input = sanitize_text_field($_POST['code']);
$current_user_id = get_current_user_id();
update_user_meta($current_user_id, 'metakey_' ,$input);
echo "<script>alert('success')</script>";
}
}
add_action('wp_head','a');
And the input value should be something like:
a:1:{i:0;s:1:"1";}
However, when I check phpmyadmin for the updated result, something different is stored, such as
s:1"a:1:{i:0;s:1:"8";}"
How should I change my code to make the stored value exactly same as my input?
I tried deleting the metakey and update the value, but still doesn't work.

Your input data is serialized twice that's why it happens, try to pass an array
$input = explode(',' , $input);
Here is the same question asked

Related

CF7 get placeholder text from php

When using CF7 (Contact Form 7) is there any way to get the placeholder text from a php function?
I’ve tried using a shortcode to call the php function, but it doesn’t work.
Here is my test:
function.php code:
add_filter( 'wpcf7_form_elements', 'do_shortcode' );
add_shortcode( 'get_placeholder', 'get_placeholder_func' );
function get_placeholder_func () {
return "Hello world";
}
CF7 template:
[get_placeholder]
[text the-field placeholder [get_placeholder]]
First line works fine and outputs the text returned from the php function.
Second line doesn't work as it only outputs a end-bracket.
I know I can do it by using js/jQuery, but it is a bit messy.
Can anybody help? Thanks :)
I'm a little unclear as to why you would want to do this, but here's a method.
add_filter( 'wpcf7_form_elements', 'cf7_replace_a_string' );
function cf7_replace_a_string( $content ) {
// Name = Form Tag Name.
$str_pos = strpos( $content, 'name="the-field"' );
// If your form field is present.
if ( false !== $str_pos ) {
$placeholder = 'this is your placeholder';
$content = str_replace( 'placeholder="placeholder"', 'placeholder="' . $placeholder . '"', $content );
}
return $content;
}
Then your form tag would look like this:
[text the-field placeholder "placeholder"]
Why don't you try the following:
Define the function
function get_placeholder_func () {
return "Hello world";
}
Then create your own input for CF7
wpcf7_add_form_tag('custom_text_input', 'wpcf7_custom_text_input');
function wpcf7_custom_text_input($tag) {
if (!is_array($tag)) return '';
$name = $tag['name'];
if (empty($name)) return '';
$placeholder = get_placeholder_func();
$html = '<input type="text" name="'.$name.'" placeholder="'.$placeholder.'" />';
return $html;
}
Then, when editing CF7 you just have to call the input created
[custom_text_input name-of-input]
This shortcode will have name-of-input name and placeholder declared by the function get_placeholder_func()
Hope it works.

How to get gravatar in wordpress post using php?

I know the get_avatar() function but its not working. Maybe its due to the complexity of the loops? Please take a look at the code below and let me know! Thanks!
function displaymeta(){
global $post;
$m_meta_description = get_post_meta($post->ID, 'my_meta_box_check',
true);
global $wpdb;
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
foreach($user_nicenames as $nice_name)
{
foreach($nice_name as $name)
{
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
echo"<li>";
echo $name. "<br>";
echo get_avatar($name->ID,50);
echo"</li>";
}
}
}
}
}
add_filter( 'the_content', 'displaymeta' );
I've tried $name,$nice_name, $user_nicenames in function get_avatar($val->ID,50); but nothing seems to work! What am I missing here?
You've already used the right function, which is get_avatar().
But the problem is that the $name as in get_avatar($name->ID,50) is not an object. Instead, it's a string, which could be the user ID or display name (i.e. the user_nicename column in the WordPress users table).
So try replacing the foreach in your displaymeta() function with the one below, where I assigned $name to $nice_name[1], and the user ID is assigned to $user_id:
foreach($user_nicenames as $nice_name)
{
$user_id = $nice_name[0];
$name = $nice_name[1];
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
echo"<li>";
echo $name. "<br>";
echo get_avatar($user_id,50);
echo"</li>";
}
}
}
Additional Note
If you remove the , ARRAY_N as in: (but you don't have to remove it. These are just extra info..)
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
then the variable $nice_name would be an object. Hence you can then access $nice_name->user_nicename like this:
$user_id = $nice_name->id;
$name = $nice_name->user_nicename;
UPDATE
In reply to your comment on the missing content, it's because you didn't capture the variable that WordPress passes through the the_content filter. And you also need to append the LI's to that $content, and finally return the modified content (i.e. $content).
So try this code (which is already utilizing the new foreach code as I provided before or above):
function displaymeta( $content ){
global $post;
$m_meta_description = get_post_meta($post->ID, 'my_meta_box_check',
true);
global $wpdb;
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
// Add the opening UL tag. Remove if not needed.
$content .= '<ul>';
foreach($user_nicenames as $nice_name)
{
$user_id = $nice_name[0];
$name = $nice_name[1];
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
$content .= "<li>";
$content .= $name. "<br>";
$content .= get_avatar($user_id,50);
$content .= "</li>";
}
}
}
// Add the closing UL tag. Remove if not needed.
$content .= '</ul>';
return $content;
}
add_filter( 'the_content', 'displaymeta' );
Hope that helps! =)

Converting a table of html inputs to PHP array

I am trying to get the information from a html table into an php array. It is very easy to do with the following method:
function getdata($table)
{
$DOM = new DOMDocument;
$DOM->loadHTML($table);
$items = $DOM->getElementsByTagName('tr');
function tdrows($elements)
{
$str = "";
foreach ($elements as $element)
{
$str .= $element->nodeValue . ", ";
}
return $str;
}
foreach ($items as $node)
{
echo tdrows($node->childNodes) . "<br />";
}
}
The problem I am now facing is the content of the table has html inputs and I want just the value of those inputs. The table is of the form:
<table>
<tr><td><input type="text" /></td><td><input type="text" /></td><td><div class="add">add</div></td></tr>
</table>
Will I be able to modify the current function to accomplish this or should I try another approach
As PHP is server-side, you will not be able to get the value of the input unless you submit this inputted value to the server, for example using post or get when the form is submitted. You should use Javascript if you want the value of the input without having to submit the form. If this doesn't answer your question please try to ask it more clearly : )

base64_encode/decode is not working correctly

I am trying to use base64_encode to display the ids differently. I am also trying to add a large number to the id so the string will look longer.
The problem is I encrypt a number to make it a string. Then when I decrypt the same string I expect the same value but in my case it is returning different values.
Why I am not seeing the same values?
This is my code:
define('IDS_SALT', 852045641596357);
function simple_encode($id){
$data = '';
$id += IDS_SALT;
$data = base64_encode($id);
$data = str_replace(array('+','/','='),array('-','_','.'),$data);
return $data;
}
function simple_decode($code){
$data = '';
$code = str_replace(array('-','_','.'),array('+','/','='),$code);
$id = base64_decode($code);
$id -= IDS_SALT;
return $id;
}
//this is returning "OC41MjA0NTY0MTU5NjM2RSsxNA.."
echo 'Encrypted: ';
echo simple_encode(7);
echo '<br />';
//this is returning 3 a NOT 7. it should return 7
echo 'Encrypted: ';
echo simple_decode( simple_encode(7) );
echo '<br />';
I just ran the same code (exactly as above) on my test server here - I received different results:
Encrypted: ODUyMDQ1NjQxNTk2MzY0
Encrypted: 7
Which looks to be correct?
Is there any other code in your test script that could be interfering?
Steve

Build php response for ajax

I'm having a logical problem with my script. The point would be to get a some rows formatted in a table but the header should not be repeated and under all the items should be outputted and than as variable passed to ajax . But I don't see how to solve this.
function abc()
{
global $mainframe;
$db =& JFactory::getDBO();
// Check for request forgeries
if(isset($this->message)){
$this->display('message');
}
// custom: generate token for ajax request
$ajax_token = JHTML::_( 'ajax.token' );
// custom end
// JRequest::checkToken( 'get' ) or jexit( 'Invalid Token' );
$letter_raw = JRequest::getVar('val');
$letter = substr($letter_raw, -1);
$response = '<div class="no-rec">not found</div>';
$html = '';
if (!empty($letter)) {
$query = " SELECT * FROM #__glossary WHERE substr(tterm,1,1) LIKE '$letter%'";
$db->setQuery( $query );
$rows = $db->loadObjectList();
if (count($rows)) {
$header='<table class="stripeMe"><tbody><thead><tr><th>Begriff</th><th>Definition</th></tr></thead><tr>';
foreach($rows as $key => $row) {
$body='<td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr></tbody></table>';
}
$response = $header.$body;
}
$html = $response;
echo $html;
}
}
What exactly is the problem? :)
You probably should not make it a function, since I guess you are just going to load the content in with AJAX?
And you should ADD to the string not override it in each row.
UPDATED, FIXED HTML ERRORS
if (count($rows)) {
// CREATE TABLE AND HEAD
$body = '<table class="stripeMe"><thead><tr><th>Begriff</th><th>Definition</th></tr></thead>';
// TBODY FOR REPEAT INSIDE
$body .= '<tbody>'
foreach($rows as $key => $row) {
$body .= '<tr><td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr>';
}
$body .= '</tbody></table>';
$response = $body;
}
$html = $response;
echo $html;
Well if you are passing the data back as HTML than this will work so your jquery would be:
$('#holderdiv').load('abc.php');
If you are using Something like .post .ajax .get, then you will need to decide what format to pass the data back with so if it's JSON then you will need to format it as such and make sure that your jQuery is told to expect such a response. I can give more help if you can be specific about your situation and what problems you are having.

Categories