WordPress Configuration: Difference between revisions

From WickyWiki
Line 104: Line 104:
== Allow subscribers to view private posts ==
== Allow subscribers to view private posts ==


This way you can use "private" to hide posts from public viewers, but show them to known users.
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:
Info:
Line 112: Line 114:
/*
/*
* Make Private Posts visible to Subscribers
* Make Private Posts visible to Subscribers
* Typically only visible to admin or editor
*/
*/
function wjv_private_posts_subscribers(){
function wjv_private_posts_subscribers(){

Revision as of 09:13, 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: no
	Pages: no
Show:
	Excerpts
	Login Form
	Registration Form
	Auto Excerpt
New Feature Settings
	Notifications & Diagnostics: no
Other Settings
	Notify admin
	Moderate registration
	Confirmation Link
	Ignore warning messages
	Enable CAPTCHA: reCAPTCHA v2
Pages
	Login: Inloggen
	Registration: no
	Profile: Inloggen
Fields
	Display: billing_phone
	Registration: first_name, last_name, billing_phone
	Required: first_name
Dialogs
	Translated
Emails
	Translated
Captcha
	Used a google account to get a Key en Secret

Note 1: 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:

Note 2: the registration link in the standard form didn't seem to work for me, so I left that empty and added a working link in the login page.

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 twentytwentyfour. Where not mentioned otherwise, the modifications in these paragraphs are inserted in the functions.php -file.

sudo nano /var/www/wpvvb/wp-content/themes/twentytwentyfour/functions.php

Include fieldvalues 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 stirng
 */
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');

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.

/**

* You can change the capability check for your specific need.
* Limiting it to only users who can "manage_options" would
* restrict it to only admins in a default WP install.
*/

if ( ! current_user_can( 'manage_options' ) ) { add_filter( 'show_admin_bar', '__return_false' ); } </source>

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
 */
// Use wp-config.php constants in code snippet
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

/**
 * 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&nbsp;date</a>';
	}
	return $actions;
}
add_filter( 'post_row_actions', 'rd_republish_post_link', 10, 2 );