How to check if a shortcode (including its params) exist? - wordpress

Is there a way to check if a shortcode exist, including its 'id' attribute ?
For example, checking if the following shortcode is active on the website: [shortcode id="3268"]
Thanks in advance !

Try var_dump on the attributes within the Shortcode.
// var dump to show shortcode attributes are working
function bartag_func( $atts ) {
$att = shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts );
var_dump($att);
}
add_shortcode( 'bartag', 'bartag_func' );
When you call the Shortcode ( [bartag] ) in this example - you will see the attributes default values (or the values you add to the Shortcode).

There is actually a shortcode API shortcode API in wordpress, allowing you to call functions and perform actions when the shortcode is present while displaying the page. If you want to check an entire website, you will have to code a plugin that goes thru all the content.

Related

wp_link_query: Classic editor link popup adds custom CPT links again and again

When I integrate my CPT into the link search of the Classic Editor, it is at first displayed correctly.
But if there are more links in the list than can be displayed at the same time (overflow) and I then scroll up and down again, the list is updated by Wordpress via Ajax and my CPT links are appended again, so that the link list becomes longer and longer.
The default Wordpress links (post, page) on the other hand do not, even though my ID and the permalink in the $results array are unique.
There is nothing more than ID, title, permalink and info in the original $results array.
How can I prevent the appending?
add_filter('wp_link_query', array($this, 'wp_link_query'), 10, 2);
[...]
public function wp_link_query($results, $query) {
[...]
$results[] = array(
'ID' => $id,
'title' => $title,
'permalink' => $permalink,
'info' => $info
);
return $results;
}
The solution was to add the custom post type to the link query args first:
add_filter('wp_link_query_args', array($this, 'wp_link_query_args'), 10, 1);
[...]
public function wp_link_query_args($query) {
$query['post_type'][] = 'my_cpt';
return $query;
}
´´´´
But you have to still use the 'wp_link_query' filter to add the "info" of the link popup table. Otherwise Wordpress outputs "Null" for the custom post type.

wp, shortcode content not visible in homepage, but visible in post

So I created this simple shortcode, which accepts a few params and it works as expected in the post page. I add a new post, implement the shortcode code and it all works great. But when I see the post in the homepage, it only shows the title not the content. Is there any wp code shortcode that I should implement in my shortcode in order to be able to see the content that the shortcode outputs, from the homepage/search results too?
function curr_func( $atts ) {
$a = shortcode_atts( array(
'am' => '',
'c1' => '',
'c2' => '',
), $atts );
$output = "The text I output";
return $output;
}
add_shortcode( 'fx', 'curr_func' );
This is the shortcode and I use it like
fx[ ...parameters ];
I have added this as a plugin. IS there any easy fix for this?

Plugin single.php not showing

I'm creating a plugin for Wordpress that creates a new post type for news and events (yes, yet another one ;-) ). The post-type is registered under the name gg_nae.
The post type works as expected; the post type can be saved and edited.
Now I want to create a custom template file. I called it single-gg_nae.php and I saved it in the same folder as the plugin-code.
If I understand the explanation on https://developer.wordpress.org/plugins/post-types/working-with-custom-post-types/ correctly that should be enough to render the post type in the custom template, but it won't. It renders the post with the single.php file from the template.
If I move the custom template to the theme-folder however, Wordpress uses the template as intended; it renders the post in the single-gg_nae.php template.
What am I doing wrong here? Should single-gg_nae.php be placed in a specific folder in the plugin-map?
edit: I already saved the permalink-structure again, but that didn't help.
only for the missing in this three below parameter u are getting error
'rewrite' => array( 'slug' => 'slider' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
try it in argument list with rest of parameters
I was thinking all wrong when approaching this issue.
I have created custom post types before and always included them in the functions.php of my Wordpress theme, therefor creating templates (archive-PostTypeName.php and single-PostTypeName.php) to show them. I assumed that was also needed when developing a CPT-plugin.
In case of a plugin, however, you have to alter the behavior of the single.php template from within the plugin using filters. I have added the following (simplified) code to my plugin:
/**
* Alter the_content if Posttype is custom post type
*
* #uses is_single()
*/
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() AND get_post_type() === 'PostTypeName') {
// Do stuff here
$content = 'This is My Custom Post Type!';
}
// Returns the content.
return $content;
}
When opening posts of the type 'PostTypeName' the content will say 'This is My Custom Post Type!', otherwise it'll show the content of the post of page.
More information about filters (and actions) can be in the Wordpress Codex

Custom-Field option not found in custom post type WP 4.6.1

I have created a custom post type call 'Movie'.Now,I want to add custom meta field in this call Movie Reviews.I have following code for custom post type.
function Create_Movies_Posttype()
{
register_post_type('Movies',
array(
'labels'=>array('name'=>__('Movies'),'singular_name'=>__('Movie')),
'public'=>true,
'has_archive'=>true,
'rewrite'=>array('slug'=>'movies'),
'support'=>array('title','custom-fields','edit'),
)
);
}
add_action('init','Create_Movies_Posttype');
Added Meta Box
function adding_custom_meta_boxes( $post ) {
add_meta_box(
'my-meta-box',
__( 'My Meta Box' ),
'render_my_meta_box',
'post',
'normal',
'default'
);
}
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes' );
Any one please help me with this custom field.
You need to use add_meta_boxes() action to register metaboxes for a post type.
add_meta_box() function to define the box, and save_post action to save it.
You'll find everything you need by reading example on Codex add_meta_boxes
You need also to add supports parameter to the register post type arguments and add custom-field in the array defining the supports for it, all the native supports can be found here

Data validation / Sanitization callback function

I added a section to the customizer of my WP theme that allows a user to change which categories display on the first page of the theme. However, when checking with the Theme Check plugin, it returned the following error:
REQUIRED: Found a Customizer setting that did not have a sanitization callback function. Every call to the add_setting() method needs to have a sanitization callback function passed.
I have no idea how to add this function to my code. If you can help, here’s the code:
http://pastebin.com/xksf3vWd
Thanks in advance!
By default, the Customizer does not handle validation and sanitization of the user input values. It is therefore essential to sanitize these values before saving them to the database.
The add_setting() method of the WP_Customizer object accepts an 'sanitize_callback' argument, that can be used to specify a sanitization callback. So, in every add_setting() call, add the sanitization callback function.
$wp_customize->add_setting( 'first_category', array(
'default' => 'Uncategorized', // The default category name.
'sanitize_callback' => 'ys_sanitize_category', // Sanitize callback function name
) );
The Sanitize callback function:
function ys_sanitize_category( $category ) {
if ( ! in_array( $category, array( 'Uncategorized', 'Blogposts', 'News' ) ) ) { // Add the names of your categories here. Use get_categories() to fetch them dynamically.
$category = 'Uncategorized';
}
return $category;
}

Resources