Comments (21)
I would like this as well. My blog software's peculiarities require the use of non-featured images for some posts.
Perhaps to keep it simple - toggle on/off pulling featured image for each post where if it is toggled off it'll pull the first attached image instead.
Thanks!
Ray
from share-on-pixelfed.
Interesting!
Perhaps to keep it simple - toggle on/off pulling featured image for each post where if it is toggled off it'll pull the first attached image instead.
On the one hand, the Mastodon plugin I based this on uses $images = get_attached_media( 'image', $post->ID );
, which may or may not include the featured image. ("Attached" images are normally the ones uploaded through that particular post's Edit screen in WP Admin, regardless of whether they're actually present in the post's content.)
On the other, if I wanted to add this, using the first (Pixelfed obviously supports just one image per post) "attached" image may actually not be the greatest choice (which image would be "first," anyway -- uploaded first, or uploaded last, or ...?) and I may just have to, like you're suggesting, go for the first "present" image.
from share-on-pixelfed.
Ahh, makes sense.
Yes, the first present image would work. It would grab the first present image and ignore the rest (with the user knowing PixelFed's only gonna use one image and the user could place that image first in the post if there's more than one).
(I'm assuming if there is a feature image, it would use that as the first present image?)
Would something like that work?
from share-on-pixelfed.
I added a hard switch (to post either the featured image or the first image inside the post content) to the settings page on a local install, and it all seems to work. I'll probably push the changes in a day or so. You could then check if it works for you. (If it turns out the chosen image type (the default would still be "featured image") is not set for a post, that post would simply not be syndicated to Pixelfed. No falling back to the other option or anything, which I find too unpredictable.
from share-on-pixelfed.
Yay! Sounds good. I'll definitely check it out whenever you have it pushed and report back. Thank you!
Ray
from share-on-pixelfed.
Latest commit should do the trick. If you're running GitHub Updater, you should see an update prompt sometime soon. If you've installed the plugin manually, you're going to have to re-download the latest ZIP and overwrite the plugin folder with its contents. (Better have that backup handy!)
Just FYI (you can safely ignore this): the image path is now filterable, too (so that a PHP developer could pretty much completely override the plugin's default behavior).
from share-on-pixelfed.
Jan,
Just to be clear --if I hard-set it to first image does this mean it will still share the featured image if it's the only one in the post?
Ray
from share-on-pixelfed.
Yes, it will share whatever image is the first image in the actual post body, regardless of whether it's featured or not.
There is only one condition: the image has to exist in WordPress's media library (so externally hosted images won't work). (Well, CDN-hosted images should work too, as long as they were uploaded to WordPress's media library and the CDN/cache plugin doesn't do anything too crazy.) If the first image happens to be an external one, it'll try the second one, and so on.
(Well, there's another condition, actually: the img
tag's src
attribute's value has to be wrapped in either single or double quotes, but that's always going to be the case if you stick with WordPress's own "Insert Media" [or similar] functionality.)
from share-on-pixelfed.
Jan,
I have the toggle set to first image and it didn't cross-post to PixelFed (featured photo post).
Let me know what I can do to help debug.
Thanks!
Ray
from share-on-pixelfed.
Hmm. You've successfully authorized WP to post to your Pixelfed account, right? Is the Pixelfed meta box present, and checked, on the Edit Post screen? Like so? (This would look somewhat different on a Gutenberg/block editor install, but the meta box should be there in the sidebar, somewhere.)
If not, have you checked the post type you're looking to share (on the Share on Pixelfed settings page)?
And, last but not least, what does the first img
tag inside your post look like? Like, if you edit the post/block that contains the image as HTML, what's the <img src="<image-url>" class="<something-something>">
look like, exactly?
from share-on-pixelfed.
Ray, I just had a quick look at your site, and on, e.g., https://alongtheray.com/photos/sunset-near-ventura-beach/, it seems the photo is set as post's Featured Image (the post has a class of has-thumbnail
) rather than part of the post itself. So if you wanted to cross-post that photo, you may have to actually choose "Featured" after all, on the settings screen. (Unless your theme's doing something really weird, at least.)
from share-on-pixelfed.
Finally had a chance to try this and it did work this time, using featured image option.
Is there still a way if there is no featured image to share the first image in the post itself? As I mentioned before, my theme has a weird system where if I want to do a wide screen image it has to be within the post itself so its CSS can extend it across the screen.
I could manually change the settings each time but that'd be a drag. Maybe the ability to select that option on the post editor itself under the PixelFed checkbox?
Thanks and my apologies for the delay,
Ray
from share-on-pixelfed.
Interestingly, I seem to have added an "image file path" filter that would make this rather easy, but is rather useless in this case, because of the hard either ... or condition (that gets executed first).
I think, however, that I could slightly shuffle some code around without breaking the current behavior, yet allowing you to override it when needed.
Although you would have to add something like the snippet below to an mu-plugin or your theme's functions.php
. (Not just yet, though, it doesn't actually work. But it could, eventually. Mostly mentioning it for my own reference.)
add_filter( 'share_on_pixelfed_image_path', function( $file_path, $post_id ) {
if ( has_post_thumbnail( $post_id ) ) {
// Post has a Featured Image. Do nothing.
return $file_path;
}
if ( ! class_exists( '\Share_On_Pixelfed\Share_On_Pixelfed' ) ) {
// Plugin not active?
return $file_path;
}
$pixelfed_handler = \Share_On_Pixelfed\Share_On_Pixelfed::get_instance()->get_post_handler();
$post = get_post( $post_id );
if ( $pixelfed_handler->has_images( $post ) ) { // Will trigger an error (for now), but we could fix that.
$file_path = $pixelfed_handler->find_first_image( $post_id );
}
return $file_path;
} );
from share-on-pixelfed.
What that would do (once I make it work) is upload the first image inside the post's body, but only when there is no featured image. ("Featured" would have to be the default setting, still.)
from share-on-pixelfed.
That could work, be glad to help test it out. Thanks!
from share-on-pixelfed.
This should work in combination with the current (0.6.1) version:
add_filter( 'share_on_pixelfed_image_path', function( $file_path, $post_id ) {
if ( has_post_thumbnail( $post_id ) ) {
// Post has a Featured Image. Do nothing.
return $file_path;
}
$pixelfed = \Share_On_Pixelfed\Share_On_Pixelfed::get_instance()->get_post_handler();
$alt_image = $pixelfed->find_first_image( $post_id );
if ( ! empty( $alt_image ) ) {
return $alt_image;
}
return $file_path;
}, 10, 2 );
Goes in functions.php
or a (must-use) plugin, whichever you prefer.
If there's a Featured Image, this snippet doesn't do anything. Else, it'll try to find the first image inside the actual post (which would have to exist in your WP Media Library, external images don't work). If it can find such an image, it'll try to upload that instead. If it can't, $file_path
(which would almost have to be an empty string at that moment) is returned unchanged (and nothing would be uploaded or shared, which is fine).
Use this in combination with the "Featured" setting and you should be good. (It would be of literally no value in combination with the "First" option.)
(Edit: Removed the class check. The hook wouldn't run anyway, right, if the plugin wasn't active?)
from share-on-pixelfed.
Had a chance to try this -- featured image is set as on, post didn't have one and an image was attached to the post. Shared to Pixelfed but nothing showed up.
https://alongtheray.com/photos/kayak-view-from-bonners-ferry/
Let me know how I can help further debug this?
Appreciate your help!
Ray
from share-on-pixelfed.
You could try to (temporarily!) enable debug logging by adding define( 'WP_DEBUG', true );
and define( 'WP_DEBUG_LOG', true );
(and perhaps also define( 'WP_DEBUG_DISPLAY', false );
to avoid seeing all sorts of warnings from showing up on your actual site) to wp-config.php
in WordPress' main folder.
That would write errors and such to wp-content/debug.log
.
Then, you could modify our little snippet like I did below. I did two things here: add a couple error_log()
calls (these should get printed to the debug.log file mentioned above), and add a bit of commented out (because untested!) code that you could try afterward.
This second bit looks for an attached image (i.e., one of the images [could be the first, or the last, not too sure but it wouldn't matter if there's just one] uploaded when editing that post) rather than "in-post" images.
That could tell us a bit more about what's going on (and provide you with an alternative that might work).
add_filter( 'share_on_pixelfed_image_path', function( $file_path, $post_id ) {
error_log( 'Is this thing even running?' );
if ( has_post_thumbnail( $post_id ) ) {
// Post has a Featured Image. Do nothing.
error_log( 'This post already has a featured image.' );
return $file_path;
}
$pixelfed = \Share_On_Pixelfed\Share_On_Pixelfed::get_instance()->get_post_handler();
$alt_image = $pixelfed->find_first_image( $post_id );
if ( ! empty( $alt_image ) ) {
error_log( 'Found ' . $alt_image );
return $alt_image;
}
error_log( "Could not find the post's first image. Multiple causes possible." );
// If that didn't work, try and find an attached image.
$attachments = get_attached_media( 'image', $post_id );
if ( empty( $attachments ) || ! is_array( $attachments ) ) {
// Nope.
error_log( 'Could not find any attached images.' );
return $file_path;
}
foreach ( $attachments as $attachment ) {
$image = wp_get_attachment_image_src( $attachment->ID, 'large' );
if ( ! empty( $image[0] ) ) {
// The "large" thumbnail.
$url = $image[0];
} else {
// Get the original image URL.
$url = wp_get_attachment_url( $attachment->ID );
}
$uploads = wp_upload_dir();
$alt_image = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
error_log( 'Finally found ' . $alt_image );
if ( is_file( $alt_image ) ) {
return $alt_image;
} else {
error_log( 'Looks like we could not find that file after all.' );
return $file_path; // Always return after the first image.
}
}
return $file_path;
}, 10, 2 );
from share-on-pixelfed.
#0 /wordpress/core/5.7/wp-includes/class-wp-hook.php(292): WINP_Execute_Snippet->{closure}('/srv/htdocs/wp-...', 5935)
#1 /wordpress/core/5.7/wp-includes/plugin.php(212): WP_Hook->apply_filters('/srv/htdocs/wp-...', Array)
#2 /srv/htdocs/wp-content/plugins/share-on-pixelfed-master/includes/class-post-handler.php(281): apply_filters('share_on_pixelf...', '/srv/htdocs/wp-...', 5935)
#3 /srv/htdocs/wp-content/plugins/share-on-pixelfed-master/includes/class-post-handler.php(219): Share_On_Pixelfed\Post_Handler->upload_thumbnail(5935)
#4 /wordpress/core/5.7/wp-includes/class-wp-hook.php(292): Share_On_Pixelfed\Post_Handler->toot('publish', 'publish', Object(WP_Post))
#5 /wordpress/core/5.7/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array)
#6 /wordpress/core/5.7/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#7 /wordpress/core/5.7/wp-includes/post.php(5014): do_action('transition_post...', 'publish', 'publish', Object(WP_Post))
#8 /wordpress/core/5.7/wp-includes/post.php(4284): wp_transition_post_status('publish', 'publish', Object(WP_Post))
#9 /wordpress/core/5.7/wp-includes/post.php(4486): wp_insert_post(Array, false, true)
#10 /wordpress/core/5.7/wp-admin/includes/post.php(419): wp_update_post(Array)
#11 /wordpress/core/5.7/wp-admin/post.php(227): edit_post()
from share-on-pixelfed.
This second log suggests an error in the callback. I was hoping it would contain "Is this thing even running?" as well as (some of) the other debug statements, but I think your search ignored those. As well as the actual error message, it seems. (It's probably just above /wordpress/core/5.7/wp-includes/class-wp-hook.php(292): WINP_Execute_Snippet->{closure}('/srv/htdocs/wp-...', 5935)
.) The other log only contains warnings related to another plugin.
You're going to want that error message to try and figure out what's happening.
Just to be clear: you should use only the snippet in #3 (comment)
Meanwhile, one thing I noticed: looks like the middle argument in apply_filters('share_on_pixelf...', '/srv/htdocs/wp-...', 5935)
is in fact a file path, which suggests the plugin itself did actually found a (featured?) image. Then again, maybe this post (5935) isn't the one you were trying to test with.
Here's some example output (this is me trying to crosspost an empty post with one [non-featured] image attached to it):
[13-Apr-2021 07:34:21 UTC] Is this thing even running?
[13-Apr-2021 07:34:21 UTC] Could not find the post's first image. Multiple causes possible.
[13-Apr-2021 07:34:21 UTC] Finally found /home/jan/public_html/wp-content/uploads/2021/04/IndieWeb-Sheep.png
from share-on-pixelfed.
If you want to, feel free to email your debug.log to jan AT boddez DOT net. See if I can find out what's up. Probably safer, too.
I suspect something went wrong copy-and-pasting, so you might wanna also send me your functions.php
or wherever you added the code. (Also, should there actually be an issue on Pixelfed's side, the debug log should contain the entire HTTP response from the server as well. But first things first.)
from share-on-pixelfed.
Related Issues (10)
- Support token renewal HOT 1
- Add Micropub syndication target
- Allow/detect (up to four) in-post images HOT 1
- Add some form of feedback
- More robust instance URL handling HOT 1
- Disable "Share on Pixelfed" per default HOT 5
- Pictures and posts are not getting through HOT 28
- Remove "revoke" button HOT 4
- Undefined array key "post_types" in share-on-pixelfed/includes/class-post-handler.php on line 489
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from share-on-pixelfed.