How to prevent giving links by wordpress to images? - php

By default, wordpress link images you enter in the editor. How to turn off this action.
Found this solution but it doesn't work.
add_filter( 'the_content', 'attachment_image_link_remove_filter' );
function attachment_image_link_remove_filter( $content ) {
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*» /></a>}'),
array('<img','» />'),
$content
);
return $content;
}

Add this code to your theme's function.php file.
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);
Thanks

In Insert Media window, you can select what you'd like your image to be linked to. Simply select None.

Related

WordPress: Loading another page/post inside the "the_content" filter creates an 500 Internal Server Error

I would like to modify a posts content with the "the_content" filter.
Inside this filter I would like to load another posts content.
This is my code:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$module = get_post( 12345 ); // load specific post
echo apply_filters( 'the_content', $module->post_content );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Sadly this creates a 500 Internal Server Error.
I guess because I created an endless loop. Do you have any idea how to get the formatted content of another post inside the "the_content" filter?
Thanks :-)
Jan
EDIT
A bit more details: I created a custom post type called "sidebars" where I edit the content with a page builder. I would like to add this sidebars with PHP.
SOLUTION
With the "Elementor" page builder you can do this:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$elementor = \Elementor\Plugin::instance();
echo $elementor->frontend->get_builder_content( 12345 );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}

Remove ‘category’ from header title on category pages in wordpress

I would like to remove the ‘Category’ from header title text on category pages in wordpress. Have tried several plugins that claim to remove these but don’t seem to work with my version.. (4.9.6)
Here a screenshot of how it looks now:
https://imgur.com/a/E2gdr2l
So would want to have only ‘News’ displayed there. Anyone knows a way of getting rid of the Category before it?
All the best and thanks in advance!
In Magazine Base theme:
The title is added from the file inc/hooks/header-inner-page.php using the function the_archive_title() like
the_archive_title('<h1 class="entry-title">', '</h1>');
to remove Category from the output you need to add the following filter in your functions.php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
});
Code credit: Here
That would fix the issue.
Just add a line into your functions.php (After WP5.5).
add_filter( 'get_the_archive_title_prefix', '__return_false' );
check in archive.php or category.php
it is loading from here
<h1 class="entry-title">Category: News</h1>
you can check and edit any of the above mentioned files.

WordPress: Displaying placeholder text by post format

I want to generate a placeholder-text in the 'content editor' if post has post format 'gallery'. But I can't get it working:
functions.php:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
if ( 'post' == $post->post_type && has_post_format('gallery')) {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project&apos;s page. Please use the fields above for text.</i>';
return $content;
}
}
This should do it:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
$format = get_post_format($post->ID);
if ( 'post' == $post->post_type && $format == 'gallery') {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project&apos;s page. Please use the fields above for text.</i>';
}
return $content;
}
Also, please make sure that your theme is set to actually have post-format support with add_theme_support and that your post has the correct post format (gallery).

wordpress multiple post thumbnail hiding class attributes

I'm using the multiple post thumbnails plugin for Wordpress and am trouble finding a way to remove the default html attributes wordpress seem to generate? When you view the html source it comes up with;
<img width="600" height="450" src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="hotplate2thumb" />
I want to remove the width and height because that's invalid code. And I want to remove the class too, to make the site look less wordpressy.
Ideally it should generate as:
<img src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" alt="hotplate2thumb" />
I've already got this in the functions.php
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
But that only works for wordpress' defult post thumbnails and not the multiple/secondary one.
I also tried implementing the code like so on the template:
<?php
if (class_exists('MultiPostThumbnails')):
echo "<img src=\"".MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image') . "\" alt=\"\" />";
endif; ?>
But that still renders a second image for all posts even if I don't have one set, so then I get broken images appearing in the theme. Not all my posts use a second image, only a few.
Thanks
Okay I solved it by adding
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
to this function
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}

Thumbnail to external link AND Thumbnail to post

Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

Categories