Wordpress Plugin development for Contact Form - php

I am developing a simple WordPress plugin for contact form. But I don't know how to save the information in database? Could you gives some references?

The answer by Haninder has a great start but doesn't store the data in a database as per the OP.
This is where it starts getting tricky, as there are many options.
You can easily store the data in various places, but none are semantically correct, or maintainable over a long period of time.
In a single option in the options table
function save_request( $data ){
$opts = get_option( 'contact_requests' );
if( ! $opts || ! is_array( $opts ) ){
$opts = array();
}
$opts[] = $data;
update_option( 'contact_requests', $opts );
}
This would mean a slow request to save and defeats the point of a database in the first place, after several hundred contact requests, also diaplying and sorting the data would get tricky.
The best way would really to have a custom database table but there is a lot to consider when going down this path.
https://code.tutsplus.com/tutorials/custom-database-tables-creating-the-table--wp-28124
Custom Post Type
This is how I would approach this problem.
perhaps you could create a custom post type, say "contact_requests" and create a post with some post meta to represent a contact request.
This way you already get a neat list in admin, and can sort and access the data quickly and easily as required. This would be stable and fast through hundreds of thousands of entries.
function save_request( $data ){
$content = '';
foreach( $data as $key => $name ){
$content .= sprintf( '%s - %s' . PHP_EOL, $key, $name );
}
$post_data = array(
'post_title' => 'Contact Request ' . esc_html( $data['name'] ),
'post_content' => $content,
'post_type' => 'contact_requests'
);
$post_id = wp_insert_post( $post_data );
//Add Post Meta Here
add_post_meta( $post_id, 'contact_name', esc_html( $data['name'] ) );
add_post_meta( $post_id, 'contact_email', esc_html( $data['email'] ) );
add_post_meta( $post_id, 'contact_message', esc_html( $data['message'] ) );
return $post_id;
}

Related

issue in new post submission with ACF on front end

I am using Advanced Custom field for a custom post type to allow user to add post from front end. My update code is working fine. Inserting new post is not working fine. Its just adding a post, but no data is saved.
I have tried following code.
function my_acf_save_post( $post_id ) {
if( $post_id != 'new_post' ) {
// Get the selected post status
$value = get_field('post_status_field', $post_id);
// Update current post
$post = array(
'ID' => $post_id,
'post_status' => $value,
'post_title' => $_POST['acf']['_post_title'],
);
// Remove the action to avoid infinite loop
remove_action('acf/save_post', 'my_acf_save_post', 20);
// Update the post into the database
$post_id = wp_update_post( $post );
// Add the action back
do_action('acf/save_post', $post_id, 20);
}else{
// Get the selected post status
$value = get_field('post_status_field', $post_id);
// Update current post
$post = array(
'ID' => $post_id,
'post_status' => $value,
'post_title' => $_POST['acf']['_post_title'],
);
// Remove the action to avoid infinite loop
remove_action('acf/save_post', 'my_acf_save_post', 20);
// Update the post into the database
$post_id = wp_insert_post( $post );
// Add the action back
do_action('acf/save_post', $post_id, 20);
}
return $post_id;
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
I am not able to figure out, where I am getting wrong. I want when a post is submitted, all data is submitted and when updated, all data must update.
Funny thing, I solved that yesterday.
When you hooking on 'acf/save_post' you running function, while custom field has old value, you sending new values with $_POST, but with get_field() you getting old values :D
So instead
$value = get_field('post_status_field', $post_id);
You need to use, in both situations, else you need press update button twice
$value = $_POST['acf']['FIELD_KEY'];
to get FIELD_KEY do
var_dump( $_POST['acf']); die();

Add inventory value to low inventory level emails

I'm trying to add the product inventory value to the low inventory level notification emails in Woocommerce.
The email notification code is on line 386 of WC_Emails class, however, it doesn't have an action so I'm not sure of the best way to add it without changing the core file.
This mail is just a function attached to a hook. You should be able to remove it and then add your own. 100% untested, so use at your own risk.
I'm not 100% sure on how to remove the callback from the woocommerce_low_stock_notification because I'm not certain how to access the WC_Emails instance.
function so_27786112_remove_notification(){
$emails = WC_Emails::instance();
remove_action( 'woocommerce_low_stock_notification', array( $emails, 'low_stock' ) );
}
add_action( 'woocommerce_init', 'so_27786112_remove_notification' );
But as Andrew suggested you can just duplicate the low_stock() callback and modify it as you'd like. I've simply added a custom function to the appropriate hook. I've really only changed the $message equation to use the get_stock_quantity() method.
function so_27786112_stock_notification( $product ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = apply_filters( 'woocommerce_email_subject_low_stock', sprintf( '[%s] %s', $blogname, __( 'Product low in stock', 'woocommerce' ) ), $product );
$sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
if ( ! empty( $product->variation_id ) )
$title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
else
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
$qty = $product->get_stock_quantity();
$message = $title . sprintf( __( ' has %s remaining in stock.', $qty ) );
// CC, BCC, additional headers
$headers = apply_filters('woocommerce_email_headers', '', 'low_stock', $product);
// Attachments
$attachments = apply_filters('woocommerce_email_attachments', array(), 'low_stock', $product);
// Send the mail
wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
}
add_action( 'woocommerce_low_stock_notification', 'so_27786112_stock_notification' );
If the first part didn't work, you should get 2 emails.
One option would be to make your own class that extends the WC_Emails class. Then copy the low_stock() method from their class into yours, which will override their function with yours. That way you can adjust your own function however you want and you won't be editing the core file.
Another option would be to add the filters you would need there and submit a pull request back to WooThemes. They may just accept it and make it part of the core.

How to create a facebook feed for wordpress?

I'm new to worpdress and completed a basic development in wordpress course, our final project it's to bring a facebook page data like states and pics, to be displayed in a word press site, to be specific to be listed in a page, I've been researching using facebook developers and found out, tha when querying this url, https://www.facebook.com/feeds/page.php?id=[pageID]&format=json y got the JSON with all de data, also I've tested in http://jsonviewer.net/ and looks good, no I'm stuck in how to make that JSON to be displayed on a page at my site.
Please need some help with this,
You can use a Shortcode for that [fb-page id="ID-NUM"], use the function wp_remote_get() to pull the feed and then convert the returned JSON into array using PHP's json_decode().
add_shortcode( 'fb-page', 'shortcode_so_25919996' );
function shortcode_so_25919996( $atts )
{
if( empty( $atts['id'] ) )
return 'Please, provide an ID';
# Request URL content.
$url = 'https://www.facebook.com/feeds/page.php?id=' . $atts['id'] . '&format=json';
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) )
return 'Error fetching the feed.';
# Response OK. Decode the response body.
$json_to_array = json_decode( wp_remote_retrieve_body( $response ), true );
# Print the array as code block. Use a loop to build the output as HTML string.
return '<pre><code>' . print_r( $json_to_array, true ) . '</code></pre>';
}
Or you can call this same function as:
<?php echo shortcode_so_25919996( array( 'id' => 'ID-NUM' ) ); ?>

All files unattached in Media Library, need a way to re-attach them

For a few months I used a plugin that automatically downloads remote images and save them. However, I found there's about 15 000 unattached images, that are actually in posts. The plugin never attached the images to the post itself.
I have no idea what to do or how to solve this. I can't do it manually it will take ages.
Is there a way to scan the images and re-attach them to the respective post?
Update: After I run the below plugin that Sergiu mentioned. The report shows:
So it does seem to pick up the images in the post. I just wish it can attach it somehow to that post ID. Is there a way to modify the code?
In the plugin below. In line 525 i removed the code:
if ( stripos( $img, $path ) !== false ) {
$response .= 'Img already in media library<br>';
continue;
}
Now it attaches the images!
Only one last issue is that it makes new copies. I can't find a way for it to not re-download them. I prefer it to just attach them.
Here is, what i think the full piece of code responsible. Please suggest modifications:
http://pastebin.com/ePERuGjt#
/**
* Extracts all images in content adds to media library if external and updates content with new url
* #param object $post The post object
* #return array|bool Post id and images converted on success false if no images found in source
*/
function extract_multi( $post ) {
$html = $post->post_content;
$path = wp_upload_dir();
$path = $path['baseurl'];
$error = 0;
$response = '';
if ( stripos( $html, '<img' ) !== false ) {
$regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
preg_match_all( $regex, $html, $matches );
if ( is_array( $matches ) && ! empty( $matches ) ) {
$new = array();
$old = array();
foreach( $matches[2] as $img ) {
/** Compare image source against upload directory to prevent adding same attachment multiple times */
$tmp = download_url( $img );
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $img, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
#unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
continue;
}
$id = media_handle_sideload( $file_array, $post->ID );
if ( ! is_wp_error( $id ) ) {
$url = wp_get_attachment_url( $id );
$thumb = wp_get_attachment_thumb_url( $id );
array_push( $new, $url );
array_push( $old, $img );
$response .= '<p><img src="'.esc_url( $thumb ).'" style="max-width:100px;" /><br>';
$response .= '<a href="'. wp_nonce_url( get_edit_post_link( $id, true ) ).'" >'.get_the_title( $id ). '</a> Imported and attached</p>';
} else {
$response .= '<span style="color:red">Upload Error: Could not upload image. Check for malformed img src url</span><br>';
$error ++;
}
}
if( !empty( $new ) ) {
$content = str_ireplace( $old, $new, $html );
$post_args = array( 'ID' => $post->ID, 'post_content' => $content, );
if ( !empty( $content ) )
$post_id = wp_update_post( $post_args );
if ( isset( $post_id ) )
$response .= 'Post Content updated for Post: '.esc_html( $post->post_title).'<br>';
return array( 'error' => $error, 'response' => $response );
} else
$response .= 'No external images found for ' . esc_html( $post->post_title ) . '<br>';
return array ( 'error' => $error, 'response' => $response );
} else {
$response .= 'Error processing images for '. esc_html( $post->post_title ) .'<br>';
return array ( 'error' => $error, 'response' => $response );
}
} else {
$response .= 'No images found for ' . esc_html( $post->post_title) . '<br>';
return array ( 'error' => $error, 'response' => $response );
}
}
I am the original poster for this problem. After a few years, I came across this old post. I dug up the solution, in the hopes that it might help anyone in the future.
This is the modified media-tools.php file:
https://pastebin.com/8iUT78aP
Just install Media Tools plugin: https://github.com/c3mdigital/media-tools-for-WordPress
and overwrite the pastebin with the media-tools.php
Then, the plugin will actually re-attached all unattached media to the proper posts, also without re-downloading the images.
I hope this helps someone, as this problem is pure hell to solve.
This plugin seems to implement a feature dealing with exactly your problem.
i would suggest doing this way:
wget your site
wget -m http://yoursite.com
this should mirror all your site.
wget WILL NOT download unatached images.
check if everything was downloaded
delete wp-content directory (i mean dir where your images are stored)
upload files downloaded by wget.
next time use: DX Delete Attached Media Plugin.
it deletes all images attached to posts, when post is deleted.

WordPress Uploading Thumbnail for Custom Post Meta

I have set up a custom post meta field for Thumbnail on each of my posts. This function is being called correctly, and everything works up until the very last line of update_post_meta
What's interesting is that I can echo out $imageURL and get the correct address, and the file uploads fine. I can even update_post_meta with any other value whether it be a string, or another variable within the function but as soon as I try to use $imageURL or $uploaded_file['url'] it just sets the post meta to a blank string.
I've used this snippet on projects that were developed with WordPress earlier than 3.1, but this one is 3.1. Could that have something to do with it? I kind of doubt it, since this seems to be one of those super weird bugs.
function tcr_save_thumbnail($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if(!empty($_FILES['tcr_thumbnail_meta']['name'])) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$uploaded_file = wp_handle_upload($_FILES['tcr_thumbnail_meta'], $override);
$post_id = $post->ID;
$attachment = array(
'post_title' => $_FILES['tcr_thumbnail_meta']['name'],
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $_FILES['tcr_thumbnail_meta']['type'],
'guid' => $uploaded_file['url']
);
// Save the data
$id = wp_insert_attachment( $attachment,$_FILES['tcr_thumbnail_meta'][ 'file' ], $post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES['tcr_thumbnail_meta']['file'] ) );
$imageURL = $uploaded_file['url'];
update_post_meta($post->ID, "tcr_thumbnail_meta", $imageURL);
}
}
This is a functionality that has already been implemented in a plugin. try it http://wordpress.org/extend/plugins/taxonomy-images/ It is so complete!

Categories