Wordpress - Custom Archive Page without pre-posts? - wordpress

Is it possible to disable preloaded posts on a Archive-Page like archive-computers.php?
Example (archive-computers.php):
<?php
/**
* The archive template file
*
* #link http://codex.wordpress.org/Template_Hierarchy
* #package WordPress
* #subpackage mydomain.de
*/
var_dump($posts);
?>
Yes, here i have already posts in $posts. In my case the default posts_per_page => 10.
The problem is i dont need this because iam using here custom-queries on this page. So, how i can disable/prevent this "unused" query. Its waste performance.

Thats solves my issue!
function _cancel_query( $query ) {
if ( !is_admin() && is_post_type_archive( 'computer' ) ) {
$query = false;
}
return $query;
}
add_action( 'posts_request', '_cancel_query' );

Related

Can I create custom post type just for documents like PDF, Video, images? WordPress

What I Have done until now, I am creating a WordPress plugin to add menu page for the document management in WordPress
<?php
/**
*
*
*
* #wordpress-plugin
* Plugin Name: doc management
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/** Step 2 (from text above). */
add_action( 'admin_menu', 'my_plugin_menu' );
/** Step 1. */
function my_plugin_menu() {
add_menu_page( 'My Plugin Options', 'docs Management', 'manage_options', 'my_unique_identifier2', 'my_plugin_options','',4 );
}
/** Step 3. */
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
$file = plugin_dir_path( __FILE__ ) . 'view/main.php';
include_once $file;
}
?>
but I need to add functionality just like in WordPress Post and pages menu. So I am thinking instead of creating a new plugin for the functionality can I do it with custom post type. instead of adding pages or post I just want to add a document. that it's nothing else means instead of adding a new page. add a new document and list all those documents. bulk delete and all.
Is there any specific need that you are trying to build a custom plugin?
I recommend, you may try using https://wordpress.org/plugins/custom-post-type-ui/

Add Wordpress Pages to their own RSS feed and not main RSS feed using Plugin

I'm learning to write Wordpress plugins and found some code that adds Wordpress pages to the RSS feed.
I used the following code in my Wordpress Plugin and it works great!
https://www.thewebtaylor.com/articles/wordpress-add-pages-rss-feed
I'm trying to figure out how to send Wordpress Pages to their own separate feed and not include it in the Wordpress default RSS.
`I found a solution for this.
add_action( 'pre_get_posts', 't5_pages_in_feed' );
/**
* Set post type to 'page' if it was requested.
*
* #param object $query
* #return void
*/
function t5_pages_in_feed( &$query )
{
if ( isset ( $_GET['post_type'] ) && $_GET['post_type'] === 'page' && is_feed() )
{
$query->set( 'post_type', 'page' );
}
}`
Now you get the page feed at /feed/?post_type=page.
https://wordpress.stackexchange.com/questions/52853/how-to-get-a-feed-for-post-type-page

How to stop Algolia from indexing posts with noindex in Wordpress

Im using Algolia on my website gintlemen.com and i dont want posts, that are set to noindex via Yoast SEO Plugin to be indexed by Algolia.
I found this post https://community.algolia.com/wordpress/indexing-flow.html, but im not sure where to put the snippet.
Can you help me with that?
To exclude posts marked as 'noindex' by Yoast, you need to add the following snippet to the functions.php of your active theme.
<?php
/**
* Don't index pages where the robot index option
* in the Yoast SEO plugin is set to noindex.
*
* #param bool $should_index
* #param WP_Post $post
*
* #return bool
*/
function filter_post( $should_index, WP_Post $post )
{
if ( false === $should_index ) {
return false;
}
return get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true) == 1 ? false : true;
}
// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'filter_post', 10, 2 );
add_filter( 'algolia_should_index_post', 'filter_post', 10, 2 );

How to create multiple content blocks in wordpress?

thanks in advance for explaining the idea of creating multiple content blocks in Wordpress post editor in the admin panel. I tried searching for similar thread before asking and couldn't find any answer.
What I need is to create an additional content field along with the default one. What functions do I need to implement please? I found a plugin "Multiple Content Blocks" in wordpress plugin library but I believe this simple task will require fewer codes. I hope I have explained well what I need. Thanks again!
Oldish question, but since it was the first hit on my google search I'll add my method - there are a few major issues with the accepted answer, primarily:
using a meta box, when the documentation clearly states that it is not safe for wp_editor().
using sanitize_text_field() to sanitize html (which removes all html!)
This file can be dropped into your theme folder or at the bottom of your functions.php, and all posts and pages will get an additional editor.
If using a separate file just remember to include it from your functions.php:
include __DIR__ . '/my_extra_content.php';
Obviously you should do a search for "my_" and replace with something meaningful - "extra_content" may also be a tad too generic, so come up with something more exotic.
<?php
//Use a class to avoid conflicts
class my_extra_content {
/**
* Called on instantiation, this is where we hook functions
*/
function __construct() {
/* Using add_meta_box seems like the correct way to do this, but since
* we're inserting a TinyMCE editor we cannot (should not) - from codex:
* ---
* Once instantiated, the WYSIWYG editor cannot be moved around in the
* DOM. What this means in practical terms, is that you cannot put it in
* meta-boxes that can be dragged and placed elsewhere on the page.
* Instead use 'edit_page_form' (for pages) or 'edit_form_advanced'
* (for other post types).
*/
add_action( 'edit_page_form', array($this, 'my_extra_content_custom_box') );
add_action( 'edit_form_advanced', array($this, 'my_extra_content_custom_box') );
/* This one saves the content */
add_action( 'save_post', array($this, 'save_postdata' ));
}
/**
* This actually outputs the tinyMCE box
*/
function my_extra_content_custom_box( $post ) {
/* Always use a nonce */
wp_nonce_field( 'my_extra_content_custom_box', 'my_extra_content_custom_box_nonce' );
/* Get the content */
$content = self::get_content($post);
/* Insert the editor */
wp_editor( $content, "my_extra_content");
}
/**
* Saves the content
*/
function save_postdata( $post_id ) {
/* Check that nonce was sent */
if ( ! isset( $_POST['my_extra_content_custom_box_nonce'] ) ) {
return $post_id;
}
/* Check that nonce is valid */
if ( ! wp_verify_nonce( $_POST['my_extra_content_custom_box_nonce'], 'my_extra_content_custom_box' ) ) {
return $post_id;
}
/* Don't try to do anything on autosave (custom fields aren't included) */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
/* Check permissions */
if ( 'page' === get_post_type( $post_id ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* Sanitize content - we don't use sanitize_text_field() as it strips all
* HTML, which is clearly not wanted with a wysiwyg - wp_kses_post()
* should do what we want */
$sane_content = wp_kses_post( $_POST['my_extra_content'] );
/* Save content - notice the underscore in the meta name - it hides the
* field from the "normal" custom field editor */
update_post_meta( $post_id, '_my_extra_content_content', $sane_content );
}
/**
* Static function makes it easy to get the value wherever you need it.
* - for example:
* $my_extra_content = my_extra_content::get_content()
*/
static function get_content($post_or_post_id = null) {
/* First find the post id */
$post_id = false;
if ($post_or_post_id === null) {
/* If nothing was passed, try to get it from global post object */
global $post;
$post_or_post_id = $post->ID;
}
if (is_a($post_or_post_id, 'WP_Post')) {
/* If a post object was passed, or we're using the global $post */
$post_id = $post_or_post_id->ID;
} elseif (is_numeric($post_or_post_id)) {
/* If a number (hopefully a post id) was passed */
$post_id = intval($post_or_post_id);
}
/* Try to get the value */
$value = get_post_meta($post_id, '_my_extra_content_content', true );
/* If we didn't get a valid string return an empty one */
if (!is_string($value)) {
return '';
}
return $value;
}
/**
* Static function to very easily output the content in a template
* - for example:
* my_extra_content::echo_content()
*/
static function echo_content( $post_or_post_id = null ) {
$output = self::get_content($post_or_post_id);
/* do_shortcode makes sure we support shortcodes (if that is wanted) */
// $output = do_shortcode($output);
/* the_content filter will apply all normal filters (including
* do_shortcode) to the content (not required!) */
$output = apply_filters( 'the_content', $output);
/* print it */
echo $output;
}
}
/* Instantiate the class - because of the static functions used to fetch the
* content we won't need to ever use this variable, we just need __construct()
* to be called, so our hooks are added */
$extra_content_throwaway_var = new my_extra_content();
First of all adding content editors to Wordpress edit pages is a lot harder than it sounds, so if you are not familiar with the save/update cycle and metaboxes then I would recommend using a plugin. I like "Advanced Custom Fields" but I'm sure "Multiple Content Blocks" is good too.
In any case I have outlined a general Custom Meta Box solution here. So here we go:
The wp_editor() function is what we use to create an editor instance. http://codex.wordpress.org/Function_Reference/wp_editor
However, I would call this within a meta box.
http://codex.wordpress.org/add_meta_box
Here is some sample code that creates a meta box with a content editor in it.
This plugin stores the value of the content editor in a custom field called _hurtigtech_extra_content that gets saved when the post/page is updated update.
You can drop this plugin into the plugins folder /wp-content/plugins/ and play with it there. Feel free to leave comments if you need help with this, I know it is a lot of code so again the plugins might be best, but this is also a good baseline if you feel confident.
<?php
/**
* Plugin Name: Extra Metabox Content Editor
*/
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function hurtigtech_add_custom_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'hrutigtech_extra_content_section',
__( 'My Post Extra Content', 'hurtigtech_translations' ),
'hurtigtech_inner_custom_box',
$screen
);
}
}
add_action( 'add_meta_boxes', 'hurtigtech_add_custom_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function hurtigtech_inner_custom_box( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'hurtigtech_inner_custom_box', 'hurtigtech_inner_custom_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_hurtigtech_extra_content', true );
echo '<br />';
wp_editor( $value, "hurtigtech_extra_content_editor");
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function hurtigtech_save_postdata( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['hurtigtech_inner_custom_box_nonce'] ) )
return $post_id;
$nonce = $_POST['hurtigtech_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'hurtigtech_inner_custom_box' ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input.
$mydata = sanitize_text_field( $_POST['hurtigtech_extra_content_editor'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_hurtigtech_extra_content', $mydata );
}
add_action( 'save_post', 'hurtigtech_save_postdata' );
NOTE: There is a style issue with the content box background. This needs to be added to an editor-style.css file to fix it.
.hurtigtech_extra_content_editor {
background: #fff;
}

Internal linking in Wordpress

If I have lots of pages... page_ids 1-100... how do I link between the two in the editor?? I guess I can use Link but that's not user friendly... I want to do something like Link but that doesn't work either. Is there a handy plugin?
Use a shortcode.
Add the following to your themes’ functions.php:
if ( ! function_exists('toscho_id_to_link') )
{
/**
* Creates a link from the post id.
*
* Usage: [link id=42 title="The Meaning of Life?" class="pseudophilosphical"]Guess![/link]
*
* Inspired by Sergej Müller
* #see http://playground.ebiene.de/2388/wordpress-shortcode-links/
* #param array $atts id (numeric) and additional HTML attributes
* #param string $data
* #return string
*/
function toscho_id_to_link($atts, $data)
{
// incomplete
if ( ! isset ( $atts['id'] ) or ! is_numeric($atts['id']) )
{
return $data;
}
// test
$url = get_permalink($atts['id']);
// No entry with this ID.
if ( ! $url )
{
return $data;
}
unset ( $atts['id'] );
$attributes = '';
// more attributes?
if ( ! empty ($atts) )
{
foreach ($atts as $key => $value )
{
$attributes .= " $key='$value'";
}
}
return "<a href='$url'$attributes>$data</a>";
}
add_shortcode('link', 'toscho_id_to_link');
}
You may find this plugin helpful: Simply show IDs.
We use RB-Internal-Links. It allows you to link using a shortcode and slug, or even has a WYSIWYG interface.
There are plugins that you can use to insert PHP in your posts or pages. Maybe using one of those will let you use your second suggestion.
You should really use complete and full URLs for all links in WordPress. http://example.com/index.php?page_id=123 , for example.
Using partial links will result in strange behaviors in feeds, on category archives, etc.

Resources