FAQ about WordPress CMS
How to remove unnecessary parts of a content generated by wordpress
WordPress CMS addes useless html parts to site content. Removing it lets you to get rid of html trash and hides what is the real cms your site is running on.
Just add the following code to functions.php or build plugin and install it.
remove_action( 'wp_head', 'wp_resource_hints', 2 ); remove_action( 'wp_head', 'wp_shortlink_wp_head' ); remove_action( 'wp_head', 'feed_links_extra', 3 ); // Display the links to the extra feeds such as category feeds remove_action( 'wp_head', 'feed_links', 2 ); // Display the links to the general feeds: Post and Comment Feed remove_action( 'wp_head', 'rsd_link' ); // Display the link to the Really Simple Discovery service endpoint, EditURI link remove_action( 'wp_head', 'wlwmanifest_link' ); // Display the link to the Windows Live Writer manifest file. remove_action( 'wp_head', 'index_rel_link' ); // index link remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Display relational links for the posts adjacent to the current post. remove_action( 'wp_head', 'wp_generator' ); // Display the XHTML generator that is generated on the wp_head hook, WP version remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('wp_print_styles', 'print_emoji_styles'); function dm_remove_wp_block_library_css(){ wp_dequeue_style( 'wp-block-library' ); } add_action( 'wp_enqueue_scripts', 'dm_remove_wp_block_library_css' ); function disable_embeds_code_init() { // Remove the REST API endpoint. remove_action( 'rest_api_init', 'wp_oembed_register_route' ); // Turn off oEmbed auto discovery. add_filter( 'embed_oembed_discover', '__return_false' ); // Don't filter oEmbed results. remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); // Remove oEmbed discovery links. remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); // Remove oEmbed-specific JavaScript from the front-end and back-end. remove_action( 'wp_head', 'wp_oembed_add_host_js' ); add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' ); // Remove all embeds rewrite rules. add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); // Remove filter of the oEmbed result before any HTTP requests are made. remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 ); } add_action( 'init', 'disable_embeds_code_init', 9999 ); function disable_embeds_tiny_mce_plugin($plugins) { return array_diff($plugins, array('wpembed')); } function disable_embeds_rewrites($rules) { foreach($rules as $rule => $rewrite) { if(false !== strpos($rewrite, 'embed=true')) { unset($rules[$rule]); } } return $rules; } function wpplugins_remove_recentcomments() { global $wp_widget_factory; remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) ); } add_action('widgets_init', 'wpplugins_remove_recentcomments'); // Disable REST API link tag remove_action('wp_head', 'rest_output_link_wp_head', 10); // Disable oEmbed Discovery Links remove_action('wp_head', 'wp_oembed_add_discovery_links', 10); // Disable REST API link in HTTP headers remove_action('template_redirect', 'rest_output_link_header', 11, 0); // Remove WP Version From Styles add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999 ); // Remove WP Version From Scripts add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999 ); // Function to remove version numbers function sdt_remove_ver_css_js( $src ) { if ( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; }
How to enable connection encryption using CloudFlare and WordPress Site on Apache
CloudFlare is a popular CDN that lets you to easily control encryption methods. By the way it allows you to use minifiers for html, css and js code.
To force SSL without setting encryption on your server set Flexible SSL/TLS encryption mode in CloudFlare, set https in Setting – General (in wp admin area) and add the following php code to wp-config.php:
if ((isset($_ENV["HTTPS"]) && ("on" == $_ENV["HTTPS"])) || (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "1") !== false)) || (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "on") !== false)) || (isset($_SERVER["HTTP_CF_VISITOR"]) && (strpos($_SERVER["HTTP_CF_VISITOR"], "https") !== false)) || (isset($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"], "https") !== false)) || (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_X_FORWARDED_PROTO"], "https") !== false)) || (isset($_SERVER["HTTP_X_PROTO"]) && (strpos($_SERVER["HTTP_X_PROTO"], "SSL") !== false)) ) { $_SERVER["HTTPS"] = "on"; }
How to add less-than sign and more-than sign?
If you try to add < or > to the content straightforwardly it may break your html markup. Even using special html codes will not solve the problem. The easiest solution is to place < or > between span tag:
<span> < </span> <span> > </span>
How add input file meta box and process post file request via custom endpoint
There is an example of a plugin that adds meta boxes to post and page editor pages. By means of these meta boxes it is posible to upload audio file (audio version of a content) to server and show it in posts and pages content.
Functional parts:
- Add meta box with input tag used to pass chosen file (input type file)
- Add custom endpoint to handle post file request and save passed file
- Add javascript code to meta box area to create post file request
Custom meta boxes
define('POST_AV_PATH', plugin_dir_url( __FILE__ )); function add_post_meta_audio_version() { add_meta_box( "post_metadata_audio_version", "Audio version", "post_meta_box_audio_version", "page", "normal", "low" ); add_meta_box( "post_metadata_audio_version", "Audio version", "post_meta_box_audio_version", "post", "normal", "low" ); } add_action( "admin_init", "add_post_meta_audio_version" ); function post_meta_box_audio_version() { global $post; $show_audio_version=get_post_meta($post->ID, '_show_audio_version', true); //adding admin area js and css shown below wp_enqueue_script( 'av', POST_AV_PATH . '/av/av.js' ,'', '1.0'); wp_enqueue_style( 'av', POST_AV_PATH . '/av/av.css' ,'', '1.0'); //generate token to be able to use custom endpoint wp_localize_script( 'Token', 'Token', array( 'nonce' => wp_create_nonce( 'wp_rest' ), ) ); if(file_exists(realpath($_SERVER["DOCUMENT_ROOT"]). POST_AV_PATH . '/files/'.$post->ID.'.mp3')) { echo '<p>File is uploaded</p>'; echo '<audio controls> <source src="'. POST_AV_PATH . '/files/' .$post->ID.'.mp3'.'" type="audio/mp3"> Your browser does not support the audio element. </audio>'; } else { echo '<p>File is not uploaded</p>'; } if($show_audio_version=='') { echo '<label>Show av:</label> <select> <option value="yes">Yes</option> <option value="no">No</option> </select>'; } else { if($show_audio_version=='yes') echo '<label>Show av:</label> <select> <option value="yes" selected>Yes</option> <option value="no">No</option> </select>'; else echo '<label>Show av:</label> <select> <option value="yes">Yes</option> <option value="no" selected>No</option> </select>'; } echo '<button type="button" id="add_av_bt">Add audio version</button> <button type="button" id="save_av_bt">Save audio version</button>'; }
Custom endpoint
Use register_rest_route function to make an endpoint which can be accessed by Post request to /wp-json/av/pushav url. Set permission_callback to function that returns true for authorized request. Request can be authorized if it contains Token written to JS by wp_create_nonce. This Token is set to X-WP-Nonce header in post file request.
post_av_handler simply adds audio file with .mp3 extension and the same name as the id of a post, if it is not already exists. If it is, then it deletes the previous file and writes the new one.
add_action( 'rest_api_init', 'add_av_api'); function add_av_api() { register_rest_route('/av', 'pushav', array( 'methods' => 'POST', 'callback' => 'post_av_handler', 'permission_callback' => function($request){ return is_user_logged_in(); } )); } function post_av_handler( WP_REST_Request $request ) { update_post_meta($request['_id'], "_show_audio_version", sanitize_text_field( $request["_show_audio_version"] )); $files = $request->get_file_params(); if ( !empty( $files ) && !empty( $files['file'] ) ) { $file = $files['file']; unlink(getcwd(). POST_AV_PATH . '/files/' . $request['_id'] . '.mp3'); copy($file['tmp_name'], getcwd() . POST_AV_PATH . '/files/' . $request['_id'] . '.mp3'); } return rest_ensure_response( ['success' => true] ); }
Show audio file in content
If _show_audio_version meta field is set to true and audio file (‘ID’.mp3) exists, audio tag will be added to the content.
function post_av_render( $content ) { if ( is_singular( ) ) { global $post; $show_audio_version=get_post_meta($post->ID, '_show_audio_version', true); if($show_audio_version=='yes') { if(file_exists(realpath($_SERVER["DOCUMENT_ROOT"]) . POST_AV_PATH . '/files/'.$post->ID.'.mp3')) { $av='<div class="av"><audio controls>'. '<source src="' . POST_AV_PATH . '/files/'.$post->ID.'.mp3'.'" type="audio/mp3"> Your browser does not support the audio element.</audio></div>'; $content=$av . $content; } } } return $content; } add_filter( 'the_content', 'post_av_render' );
JS for admin area
FormData is used to store necessary data which is to be send. Also a header named X-WP-Nonce should be written to request and have value of Token.nonce (that is rendered on backend).
document.addEventListener("DOMContentLoaded", function() { //adding input element from string to document on click document.getElementById("add_av_bt").addEventListener("click",AddFileForm); //save choosen file posting it via wp endpoint document.getElementById("save_av_bt").addEventListener("click",SaveFileForm); }); function AddFileForm() { var xmlString="<input type='file' id='post_av' name='file'>"; $( xmlString ).insertAfter( "#add_av_bt" ); } function SaveFileForm() { var selection= $('#post_metadata_audio_version select')[0].value; var id=document.getElementById("post_ID").value; var formData = new FormData(); formData.append('_show_audio_version', selection); formData.append('_id', id); var fl=document.getElementsByName('file')[0]; if(fl) formData.append('file', fl.files[0]); $.ajax({ url: '/wp-json/av/pushav', data: formData, type: 'POST', contentType: false, processData: false, beforeSend: function(xhr){xhr.setRequestHeader('X-WP-Nonce', Token.nonce);}, }); document.getElementById("post_av").remove(); }
CSS for admin area
#post_metadata_audio_version p { width: 100%; text-align: center; font-weight: bolder; font-size: large; } #post_metadata_audio_version audio { width:100%; } #post_metadata_audio_version * { margin: 10px; }