WordPress Configuration: Difference between revisions
| Line 169: | Line 169: | ||
== Add duplicate-button to posts == | == Add duplicate-button to posts == | ||
You can pick a post that has most of the make-up you want to use, duplicate and edit it. | |||
<source lang=php> | <source lang=php> | ||
Revision as of 15:39, 16 November 2024
Links
WordPress Plugins
BackWPUp (BackWPup)
I use this for backups, didn't try restore yet at this point however.
WP-Members (Chad Butler)
WP-Members Options:
- Content Restriction: Posts: not blocked
- Private posts are used for subscribers, see modifications and remarks below
- Content Restriction: Pages: blocked
- Per-page visibility setting
- Show: none
- Auto summary: none
- New Feature Settings: default (no)
- Other Settings: Memberships: no
- Other Settings: Notify admin: yes
- Other Settings: Moderate registration: yes
- Other Settings: Confirmation Link: yes
- Other Settings: Ignore warning messages: yes
- These are mainly ads and updates
- Pages: Login: "Inloggen"
- Pages: Registration: "Registreren"
- Pages: Profile: "Profile"
- is needed for the password reset link to work, and also for the links in the E-mails
- Fields: minimum fields and translation
- Dialogs: translation
- Emails: translation
- Other Settings: Enable CAPTCHA: reCAPTCHA v2
- Captcha: Used a google account to get a Key en Secret
- with reCAPTCHA v3 you will get an autopmatic check and icon on every page of your website. With v2 you will only see it on the registration page.Google: https://www.google.com/recaptcha/admin#whyrecaptcha
WordPress Modifications to "functions.php"
The file 'functions.php' is part of the thema you are using and should be in the thema's folder. The thema here is twenty-twenty-four-wjv. Where not mentioned otherwise, the modifications in these paragraphs are inserted in the functions.php -file of this theme. Make sure you create and use a child-theme because any changes will be overwritten when the thema is updated.
sudo nano /var/www/wordpress/wp-content/themes/twenty-twenty-four-wjv/functions.php
Include field values in page text with shortcode
This allows you to include a first_name or other fields in a weppage by using a shortcode.
/**
* User Meta Shortcode handler
* usage: [USER_META user_id=1 meta="first_name"]
* or: [USER_META meta="first_name"]
* @param array $atts
* @param string $content
* @return string
*/
function wjv_meta_shortcode_handler($atts,$content=null) {
if (isset($atts['user_id']) || array_key_exists('user_id', $atts)) {
$c_user_id = $atts['user_id'];
}
else {
$c_user_id = wp_get_current_user()->ID;
}
return esc_html(get_user_meta($c_user_id, $atts['meta'], true));
}
add_shortcode('USER_META', 'wjv_meta_shortcode_handler');
Use Custom Profile for non-admin users
After a normal user is logged in to the website the WordPress toolbar would appear. The following wil hide the toolbar in this case.
/**
* Custom profile for subscribers, this makes sure all Profile-links go to your designated page.
*/
add_action( 'load-profile.php', 'rpa_customprofile_check' );
function rpa_customprofile_check() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_redirect( add_query_arg( array( 'pcustomprofile' => 1), site_url('?page_id=269&a=edit') ) );
exit();
}
}
Hide toolbar
After a normal user is logged in to the website the WordPress toolbar would appear. The following wil hide the toolbar in this case.
/**
* Limiting toolbar to only users who can "manage_options"
* such as admins in a default WP install.
*/
if ( ! current_user_can( 'manage_options' ) ) {
add_filter( 'show_admin_bar', '__return_false' );
}
Allow subscribers to view private posts
Blocked posts would still be visible as a title and a summary to the public, you can limit the summary with the "more" tag. Clicking the post brings a signed-in subscriber to the whole content or else to a sign-in message.
By allowing "private" posts to signed-in members you can still hide them completely from the public but show them to subscribers.
Info:
/**
* Make Private Posts visible to Subscribers
*/
function wjv_private_posts_subscribers(){
$subRole = get_role( 'subscriber' );
$subRole->add_cap( 'read_private_posts' );
$subRole->add_cap( 'read_private_pages' );
}
add_action( 'init', 'wjv_private_posts_subscribers' );
Add Email function
WP-Members uses the native WP function wp_mail to send all emails. This is a native WordPress function and is generally robust.
Info:
/**
* E-mail function
* NB: constants in wp-config.php
*/
function wjv_send_smtp_email( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASSWORD;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_FROMNAME;
$phpmailer->Port = SMTP_PORT;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->SMTPDebug = SMTP_DEBUG;
}
add_action( 'phpmailer_init', 'wjv_send_smtp_email' );
The best place for configuration is "wp-config.php":
sudo nano /var/www/wpvvb/wp-config.php
/** * Define constants for SMTP settings * SMTP Port for SSL: 465 * SMTP Port for TLS/STARTTLS: 587 */ define( 'SMTP_HOST', 'smtp.mail.yahoo.com' ); define( 'SMTP_USER', 'user1@yahoo.com' ); define( 'SMTP_PASSWORD', '****' ); define( 'SMTP_FROM', 'user1@yahoo.com' ); define( 'SMTP_FROMNAME', 'WordPress - user1@yahoo.com' ); define( 'SMTP_PORT', '587' ); define( 'SMTP_AUTH', true ); define( 'SMTP_SECURE', 'tls' ); define( 'SMTP_DEBUG', 2 );
Add duplicate-button to posts
You can pick a post that has most of the make-up you want to use, duplicate and edit it.
/**
* Duplicate a post
*/
function dt_dpp_post_as_draft() {
global $wpdb;
/*sanitize_GET POST REQUEST*/
$post_copy = sanitize_text_field( $_POST["post"] );
$get_copy = sanitize_text_field( $_GET['post'] );
$request_copy = sanitize_text_field( $_REQUEST['action'] );
$opt = get_option('dpp_wpp_page_options');
$suffix = !empty($opt['dpp_post_suffix']) ? ' -- '.$opt['dpp_post_suffix'] : '';
$post_status = !empty($opt['dpp_post_status']) ? $opt['dpp_post_status'] : 'draft';
$redirectit = !empty($opt['dpp_post_redirect']) ? $opt['dpp_post_redirect'] : 'to_list';
if (! ( isset( $get_copy ) || isset( $post_copy ) || ( isset($request_copy) && 'dt_dpp_post_as_draft' == $request_copy ) ) )
{
wp_die('No post!');
}
/* Get post id */
$post_id = (isset($get_copy) ? $get_copy : $post_copy );
$post = get_post( $post_id );
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*
* if post data exists, create the post duplicate
*/
if (isset( $post ) && $post != null) {
/* Post data array */
$args = array('comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $post_status,
'post_title' => $post->post_title.$suffix,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
/*
* insert the post by wp_insert_post() function
*/
$new_post_id = wp_insert_post( $args );
/*
* get all current post terms and set them to the new post draft
*/
$taxonomies = get_object_taxonomies($post->post_type);
if(!empty($taxonomies) && is_array($taxonomies)):
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);}
endif;
/*
* duplicate all post meta just in two SQL queries
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
/*
* redirect
*/
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
}
else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'dt_dpp_post_as_draft' );
//Add the duplicate link to action list for post_row_actions
function rd_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
Add Republish-button to posts
/** wjv
* add republish action to Posts
*/
function dt_dpp_republish() {
$post_copy = sanitize_text_field( $_POST["post"] );
$get_copy = sanitize_text_field( $_GET['post'] );
$post_id = (isset($get_copy) ? $get_copy : $post_copy );
$request_action = sanitize_text_field( $_REQUEST['action'] );
if (! ( isset( $get_copy ) || isset( $post_copy ) || ( isset($request_action) && $request_action == 'dt_dpp_republish' ) ) )
{
wp_die('No post!');
}
$my_post = array(
'ID' => $post_id,
'post_date' => current_datetime()->format( 'Y-m-d H:i:s' ),
'post_date_gmt' => gmdate( 'Y-m-d H:i:s' ),
);
wp_update_post( $my_post );
wp_redirect( admin_url( 'post.php' ) );
}
add_action( 'admin_action_rd_republish', 'dt_dpp_republish' );
// Add the republish link to action list for post_row_actions
function rd_republish_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['republish'] = '<a href="' . wp_nonce_url('admin.php?action=rd_republish&post=' . $post->ID, basename(__FILE__), 'republish_nonce' ) . '" title="Refresh the publish date" rel="permalink">Republish date</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'rd_republish_post_link', 10, 2 );