I'm trying to add custom plugin after single post content. I have tried with add_filter and add_action to get my plugin printed out.
if(!defined('ABSPATH')) exit;
function customPlug_plugin_install()
{
}
register_activation_hook(__FILE__, 'customPlug_plugin_install');
function customPlug_plugin_scripts()
{
wp_register_script('customPlug_script', plugin_dir_url(__FILE__), 'js/customplug.js', '1.0', true);
wp_register_script('customPlug_bootstrap_script', plugin_dir_url(__FILE__), 'js/customplug.js', '1.0', true);
wp_register_script('customPlug_script', plugin_dir_url(__FILE__), 'js/customplug.js', '1.0', true);
wp_enqueue_script('customPlug_script');
}
function my_plugin($content) {
$content = "Custom Plugin Content";
return $content;
}
add_action('the_content', 'my_plugin');
However, this is just returning either the plugin content, or the_content if I comment out add_filter or add_action
You're overwriting current content. You should concatenate your content to current content with .
function my_plugin($content)
{
$content. = "Custom Plugin Content";
return $content;
}
Related
Please consider the code:
function mcqac_wp_enqueue_assets() {
if (is_admin()) {
wp_enqueue_script(
'mcqac-js-admin', // Handle.
PLUGIN_URL . 'build/main-admin.js',
array( 'jquery' ), // Dependencies, defined above.
filemtime( PLUGIN_PATH . 'build/main-admin.js' ), // Version: File modification time.
true // Enqueue the script in the footer.
);
$mcqacAdminData = array();
if (get_the_ID()) {
$mcqacAdminData['options'] = get_post_meta(get_the_ID(), 'mcqac_options', true);
}
wp_localize_script('mcqac-js-admin', 'mcqacAdminData', $mcqacAdminData);
}
}
add_action('init', 'mcqac_wp_enqueue_assets');
The get_the_ID() does not return anything when I am in the edit post page. Seems like init action hook is fired before the post query.
What is the solution?
Problem fixed the admin_enqueue_scripts action hook instead of init action hook. And also declare global $post to get post id from this variable.
Try the template_redirect hook instead of init.
add_action('template_redirect', 'prefix_get_page_id');
I want to redirect admin users to a maintenance page (https://mysite/maintenance/), but firefox tells me the redirection is not correctly made
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
Have you an idea ?
You should be using the template_include filter for this:
add_filter('template_include', 'wpse_44239_template_include', 1, 1);
function wpse_44239_template_include($template){
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
return $template;
}
template_redirect is the action called directly before headers are sent for the output of the rendered template. It's a convenient hook to do 404 redirects, etc... but shouldn't be used for including other templates paths as WordPress does this innately with the 'template_include' filter.
template_include and single_template hooks deal ONLY with the path of the template used for rendering the content. This is the proper place to adjust a template path.
What is the specific error Firefox is reporting? Sounds like it could be an infinite redirect loop. I would suggest adding a check to make sure you're not already on the maintenance page, ie:
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator'))
{
global $wp;
$current_url = home_url( $wp->request );
$position = strpos( $current_url , '/maintenance/' );
if ($position===FALSE) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
}
I'm facing a weird problem when using the Elementor Wordpress Page Builder.
After creating a custom shortcode and inserting it into any page position, it also shows up at the top of the page, but only in Edit mode.
Top of the page:
Place where I want to insert shortcode:
This answer on an unrelated site helped me solve this Elementor issue. https://wp-types.com/forums/topic/shortcode-output-showing-up-in-the-wrong-place/
I just had to include ob_start(); and $content = ob_get_clean(); return $content; in my function. Here is what it looks like:
function custom_author_link_function() {
ob_start();
coauthors_posts_links();
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_author_link', 'custom_author_link_function');
This is my working example:
function name_it( ){
ob_start();
function_name();
$content = ob_get_clean();
return $content;
return function_name();
}
add_shortcode( 'shortcode_name', 'name_it' );
Just look at function_name(); and return function_name(); lines to avoid errors.
I have a website that uses worpdress with catch box theme, and I want to change the 404 page title.
I looked for the "Nothing found for" sentence on the PHP files on the Editor but I did not find nothing.
I searched for the get_header(); method in editor to change the title but I did not find it.
see the title that I want to change
Have been already answered here: https://wordpress.stackexchange.com/questions/30873/how-to-change-404-page-title
Add the following to theme functions.php file
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TITLE TEXT HERE';
}
// You can do other filtering here, or
// just return $title
return $title;
}
// Hook into wp_title filter hook
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
You can use filter hook
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TEXT HERE';
}
return $title;
}
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
try this one for wordpress version 4.4+
file : function.php
function theme_slug_filter_wp_title($title_parts)
{
if (is_404()) {
$title_parts['title'] = 'My Custom Title Text';
}
return $title_parts;
}
add_filter('document_title_parts', 'theme_slug_filter_wp_title');
I would like to remove the '[...]' from the RSS feed widget in my Wordpress site and replace it with '...'.
I have tried adding the following to functions:
function replace_ellipsis($text) {
$return = str_replace('[...]', '...', $text);
return $return;
}
add_filter('get_the_excerpt', 'replace_ellipsis');
However, this does not affect the widget.
Any help?
This is defined in v3.4.2 in wp-includes/default-widgets.php, lines 848 - 862. The only function that can be hooked into that I can see is esc_html, so here's what I use:
add_filter('esc_html', 'transform_ellipsis');
function transform_ellipsis($s_html) {
return str_replace(' […]', '…', $s_html);
}
It says here: http://codex.wordpress.org/Function_Reference/the_excerpt_rss that it hooks to 'the_excerpt_rss', so perhaps this will work:
function new_excerpt_more( $excerpt ) {
return str_replace( '[...]', '...', $excerpt );
}
add_filter( 'the_excerpt_rss', 'new_excerpt_more' );
if that doesn't work, see this page for more ways: http://codex.wordpress.org/Function_Reference/the_excerpt#Remove_.5B....5D_string_using_Filters