Linkedin Url Validation PHP - php

I have a form in forminator there is a field for LinkedIn URL. I have to validate that field only accepts LinkedIn URLs using PHP. I tried
^http[s]?:\/\/www\.linkedin\.com\/(?:in|pub|public-profile\/in|public-profile\/pub)\/(?:[\w]{6}-[\w]{1,}-[\w]+)$
this code. I'm using WordPress as the backend so in forminator I'm using a forminator custom submit error hooks
add_filter(
'forminator_custom_form_submit_errors',
function( $submit_errors, $form_id, $field_data_array ) {
$field_name = 'text-1';
$submitted_fields = wp_list_pluck( $field_data_array, 'value', 'name' );
$pattern = '^http[s]?:\/\/www\.linkedin\.com\/$';
if( !preg_match( $pattern, $submitted_fields[ $field_name ] ) ) {
$submit_errors[][ $field_name ] = __( 'There is something wrong in url ' );
}
return $submit_errors;
},
10,
3
);

Related

Changed email text after updating password for user in Wordpress Dashboard User settings does not work

I need to adjust text for email sent after updating password for user in Wordpress backend.
I did it in functions.php of my theme:
add_filter('password_change_email', 'change_code');
My function:
function change_code($pass_change_mail,$user,$userdata ){
$new_message_txt = __( 'Some text ###USERNAME### more text even more text ###EMAIL### more text after more text last bit of text ###SITENAME###' );
$pass_change_mail[ 'message' ] = $new_message_txt;
return $pass_change_mail;
Actually I need to connect with this filter in user.php, which envoke somethoing like that:
$pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
$pass_change_email['message'] = str_replace( '###USERNAME###', $user['user_login'], $pass_change_email['message'] );
$pass_change_email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $pass_change_email['message'] );
$pass_change_email['message'] = str_replace( '###EMAIL###', $user['user_email'], $pass_change_email['message'] );
$pass_change_email['message'] = str_replace( '###SITENAME###', $blog_name, $pass_change_email['message'] );
$pass_change_email['message'] = str_replace( '###SITEURL###', home_url(), $pass_change_email['message'] );
wp_mail( $pass_change_email['to'], sprintf( $pass_change_email['subject'], $blog_name ), $pass_change_email['message'], $pass_change_email['headers'] );
Maybe that code is not corrected and something better exists. Someone know, how to solve that, please?

Wordpress Plugin development for Contact Form

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;
}

Output HTML using existing PHP in wordpress functions.php

I am using the below to send an email to a user each time a custom field within their profile is updated. I can output plain text into the email body using the $message line which is great. How can I adapt this so I can output html where the $message goes?
// IF CUSTOM FIELD CHANGES
function sr_user_profile_update_virtuosity( $user_id, $old_user_data ) {
$old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
$user = get_userdata( $user_id );
if($old_user_data->virtuosity != $user->virtuosity) {
$admin_email = $user->user_email;
$message = sprintf( __( 'I want to output HTML here' ) ) . "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( 'IMPORTANT: Your newly purchased product is ready for you' ), get_option('blogname') ), $message );
}
}
add_action( 'profile_update', 'sr_user_profile_update_virtuosity', 10, 2 );
You need to set the content type to html, by default its text/plain.
You can do this by using wp_mail_content_type filter.
add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
return 'text/html';
}
For more info see here.

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.

Categories