• Resolved san99

    (@san99)


    I have been trying to change url/permalink of the attachment pages in twenty sixteen for days with no success, I have tried many codes which have been shared on this website but nothing seems to be working for me. I am running some photography websites on wp, I want to change the permalink structure and want it to look cute, short and sensible. Here are some of the formats I am trying to achieve. structure 1:

    sitemamdotcom/images/attachment-title(i want to trim the title to three to four words only because long title does not look good).
    Structure 2:

    sitemamedotcom/z/5digits/attachment-title-3words/5digits
    (random 5 digits in both sides not the same, different digits in all attachment)
    structure 3:

    sitenamedotcom/attachment-title-3words/attachment-id/
    I want to show attachment pages as individual posts, the long url/slug making it difficult for me to copy the link and share.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Moderator bcworkz

    (@bcworkz)

    You can alter the attachment title itself with the ‘wp_insert_attachment_data’ filter. The slug used in permalinks is keyed under ‘post_name’.

    You can add arbitrary data in a permalink in front of the title slug with a filter such as ‘the_permalink’. As long as WP cannot associate this data with anything, it’ll be ignored and the post/attachment named will be returned. So adding 5 digits in front is usually not a problem. Adding after is a problem. For that you’ll need to formally define a rewrite rule so WP knows how to handle the extra data.

    Thread Starter san99

    (@san99)

    Hi thanks for answering the question, well I forgot to mention that I could only do modifications within theme/wordpress and for i needed the codes. I am not a programmer myself so the best i can do is copy and paste the codes within php files in the wordpress. I will appreciate if you could assist me with the code.
    If adding five digits at the start and end of attachment page is tricky, I would like to know a way to add second/time structure for instance in post permalink we can add %monthnum%%seconds%etc to add numbers and the structure always results in different/random numbers in the permalink, can we do the same with attachment page link.

    Thanks in advance

    Moderator bcworkz

    (@bcworkz)

    As a general rule I don’t code for people here, but I made an exception for you since it’s fairly simple code. Simply altering the title slug is the easiest approach by far.

    add_filter('wp_insert_attachment_data', 'bcw_get_post_data', 10, 2 );
    function bcw_get_post_data( $data, $postarr ) {
       preg_match_all('/(.+?)[-_\s]/', $data['post_name'], $matches);
       if ( 3 < count( $matches[1])) {
          $data['post_name'] = '';
          for ($i = 0; $i <= 2; $i++) {
             $data['post_name'] .= $matches[1][$i] . '-';
          }
       } else {
          $data['post_name'] .= '-';
       }
       $data['post_name'] .= strtotime( $data['post_date']);
       return $data;
    }

    This code can go on functions.php of your theme, though you should create a child theme if you’re able and haven’t already done so.

    The code truncates filenames longer than 3 words down to 3, then appends a Unix timestamp corresponding to the post date. Only attachments added within 1 second of each other will have the same timestamp. A typical title slug might look like big-foo-bar-1470924346. You can append other values besides a timestamp by replacing the call to strtotime() with a different function that generates what ever you want to append to the title slug.

    Thread Starter san99

    (@san99)

    Thank you very much for writing this code for me, this code is working and I appreciate you have done coding here (just for me:) ).
    I have used this code in the function.php file, it is working fine, random numbers are showing on the attachment url, but the attachment page is still showing with the parent post.
    for instance.
    sitename.com/parentpost/attachment-numbers/
    My last question is whether or not i can change permalink to
    sitename/attachment-numbers/ to make it pretty.
    thank you very much in advance.

    Moderator bcworkz

    (@bcworkz)

    Yes, I believe so. Try this code added to the previous:

    /* remove parent post from attachment permalink
       requires adding attachment post type to single queries
     */
    add_filter('attachment_link', 'bcw_fix_permalink', 10, 2 );
    function bcw_fix_permalink( $link, $post_id ) {
       preg_match('!/([^/]+)/?$!', $link, $slug );
       return esc_url( home_url("/$slug[1]/"));
    }
    // include attachments in single queries
    add_action('pre_get_posts', 'bcw_add_attachment');
    function bcw_add_attachment( $query ) {
       if( $query->is_main_query() && $query->is_single())
          $query->set('post_type', array('page','post','attachment',));
    }

    The last line ($query->set...) will need modification if you have custom post types that require single views. This line affects all single view queries, so be sure all such links on your site still work correctly.

    Thread Starter san99

    (@san99)

    Hi there,
    Thank you very much for sharing this code, it is working perfectly fine. No doubt in my mind that you are a pro coder and developer. Thank you for helping me with this much detailed coding and I really appreciate your giving me favor and doing coding only for me, I wish i could do something in return.:)

    Moderator bcworkz

    (@bcworkz)

    You being pleased is reward enough ?? While I’m qualified to be a pro coder, it’s not how I make a living. Making a living as a free lance coder is hard, which is why I generally don’t code for people here. They should turn to a pro if they cannot code for themselves. I also know it can be hard to hire someone for a small job, so I’ll work up little bits of code from time to time for people here to help support the WP community.

    If you are serious about giving back to the community, there’s lots of ways you can do that, even if you don’t code. See https://make.www.ads-software.com.

    Thread Starter san99

    (@san99)

    thanks for sharing the link:) i will try to participate there

    Thread Starter san99

    (@san99)

    hi there again I am sorry i need to ask a question about schema markup, can you tell me who can provide the code for me, I have asked this question on this forum but there is no reply as yet.
    There is a different code for wordpress images and attachment in the screenshot where all sizes of images are being shown along with schema markup. B default wordpress creates five size for the images, the same is here but it’s just that there is title one time and alt tags four to five times in the script. how to do it, i have tried many codes but could not get it done yet.The example can be seen in the screenshot, the schema markup is showing with all the images
    screenshot
    let me know if this script is paid so i can buy it.
    thanks

    • This reply was modified 8 years, 1 month ago by san99.
    Moderator bcworkz

    (@bcworkz)

    I’m happy to help where I can.

    The only schema that does not show content on a page is the JSON-LD form. It hides schema from end users by placing the schema within script tags. You can add arbitrary content when an image is inserted in a post with the ‘image_send_to_editor’ filter. All sorts of image data is passed to the callback, but schema likely needs more than that. I’m not sure where that would come from.

    I’ve not done it, but it’s possible to add fields to the Insert Media dialog where users could enter any additional schema data needed. The data could be saved with the attachment post meta, which would be available to the ‘image_send_to_editor’ callback.

    I’m not going to be able to code something up for you this time. You can get professional help at jobs.wordpress.net though.

    I’m sorry your other posts here have not gotten any replies. Success can be hit or miss depending on who’s around, what information is needed, and where you post in these forums. You can’t do much about the first two, but the last one is important too. Any question involving custom code is fair game in this Hacks sub-forum, though some level of coding ability is expected (though not required). This is not a get code for free forum, but generous volunteers have been know to provide some anyway.

    Thread Starter san99

    (@san99)

    Hi again,
    It seems you are the only one active on the forum right now, ?? kidding. I know many developers are providing support here, but i rarely get answers here I still try.
    As for the html front end I was referring to a particular website, i guess the link was removed by someone not by me, here is an example example If you visit the stylesheet you will notice that the site is using html theme but it is being managed on wordpress platform. Installing wordpress on a sub folder is a common thing but it seems that the developer is doing something different with html design. inholl dot com/wp-admin ( there are many sites like this one where html template is being used on WP,I don’t know how to do it and I am curious). I know this is a not a get a free code forum but I always ask questions here when I am sure that I am not getting solutions elsewhere, for instance I have tried Fiverr and many places, I hired people for the job but they would cancel the order sooner or later stating that they were not able to figure out how did the designer do it.
    I have one code for the schema (I know there are some plugins which are adding schema to the featured image only but I want to apply schema on a photography website to improve SEO).I am experimenting wit different codes right now and I have one that works on new attachments and here it is:

    add_filter( 'wp_get_attachment_image_attributes', 'wpse_235266_image_attributes', 10, 3 );
    
    function wpse_235266_image_attributes( $attr, $attachment, $size ) {
    
        $attr['itemprop'] = 'image';
        return $attr;
    
    }
    For images that have been inserted into post content in the visual editor, you'll need to filter on the get_image_tag hook to change the HTML that is inserted into each post.
    
    add_filter( 'get_image_tag', 'wpse_235266_image_html', 10, 6 );
    
    function wpse_235266_image_html( $html, $id, $alt, $title, $align, $size ) {
    
        return str_replace( '<img ', '<img itemprop="image" ', $html );
    
    }

    For instance this code only add itemprop=image attribute to the images it works differently though it does. It is missing the complete schema structure I am looking for (the one I have already shown). I want to get at least two sizes of wordpress attachments in the source page with alt and titles.. The example I have shown you contains four or five links which are spammy for certain i want to get two at the maximum.
    Thanks for the sharing the link I will sign up there right now.
    Thanks for helping me out.

    Moderator bcworkz

    (@bcworkz)

    I’m glad your forum experience wasn’t a total zero ??
    There’s no obligation for anyone to answer questions, many volunteers will only answer questions that interest them. And of course, if one doesn’t know the answer, we’re not contributing anything by replying. We are not necessarily experts here, but we do have WP experience and want to contribute to the community. Unfortunately, some questions are going to remain without any replies.

    Without access to the source files, I can only speculate about your example site. I’m guessing that static .html pages are converted into WP page templates. It’s conceivable this process could be done automatically by code. Each site page ends up being a WP page based on a unique page template. Since its origin is HTML and not WP, the usual WP signatures we expect to see do not exist.

    This allows them to use the WP permalink system while still having the ability to fully manage the page’s HTML. It appears the site also makes use of the WP commenting system.

    The schema concept was never intended to be contained entirely within img attributes. The idea was more the supporting information that would normally accompany an object could be annotated in a consistent way for machine processing. The problem is many people don’t want to display all the schema data on a page. One solution is to use CSS to hide schema data you want available but don’t want to be visible. The other solution is to use the JSON-LD format, which doesn’t display anything to the end user.

    Since the schema doesn’t need to be within the img attributes, you can use the ‘image_send_to_editor’ filter to add schema content of any type you choose. The img HTML is passed to your callback, so you merely concatenate the schema to the passed HTML and return the combined data.

    The part I’m unclear about is where you’re getting the schema data from. If you’ve figured out that part, outputting schema along with the img HTML is quite straight forward.

    If you are referring to the number of alternate image sizes provided for each inserted image, I can’t imagine this is considered spammy. The alternate sizes helps browsers load images more efficiently based on the device’s viewport size and resolution. This is primarily for more efficient mobile usage, which Google is pushing in a big way. I can’t imagine why you’d be penalized for being mobile friendly. In any case, it is possible to change the image sizes WP produces with each upload.

    Thread Starter san99

    (@san99)

    I have tried posting questions on other forums whenever something comes up about WordPress, I am using this platform more than bloggers because it is easy to work with. First of all I should say thank you for answering back to my all of the questions:) And thank you for putting up with me hehe. I am sorry I am not a coder so whenever i feel intrigued about something I come here and shoot my questions, I am glad that Pro Coders like you exist on the planet to make things easier for the naive and novice people like I am. So yea my experience is not zero on WP at all.
    I have just found out that some html template can be operated with wordpress they are edited and then integrated with wp and when this is done all some wp folders and files don’t show,there are also some plugins like hide wp which help hide wp from the source page. here is another site for review inholl dot com…. and another one artnmeal dot com… these are html on wordpress, I don’t think that html pages are converted into wordpress because I can clearly see automated blogging here so yea it is html on wordpress.
    You are right the schema concept was never meant to be contained within images only—-the schema markup is now applied in header, blogpost/article, breadcrumbs and in many other files within wordpress theme to make it SEO friendly, luckily it’s easy to edit theme for the schema except for the image attachments that’s why I am here. Some plugins add this markup with the featured images easily but none of the plugins that I have tried so far add schema to attachment pages on single or attachment pages of the blog.
    I am particularly looking for the schema for the images because this will come handy in photography blog. I have tried on many places but I could not find the solution yet, it must have been a custom function or script of few lines but I may need help to figure it out.
    You are right getting multiple links for the same image is not spammy and we can customize the sizes in uploads but the trick is how to show all of these in source page separately with alt tag each time.. it is also tricky isn’t it:)

    Moderator bcworkz

    (@bcworkz)

    I see now that I’ve misinterpreted what you meant by multiple images. I know it’s represented in your screenshot. TBH, it’s too difficult for me to make sense of minified HTML in very long lines. How is this content added to the post content? The “image_send_to_editor” filter typically only would work if images are inserted through the media library. Like I said, the crux is getting the schema data to place with the image. Once you have that, the following code will insert the data with the image.

    add_filter(‘image_send_to_editor’, 'san_add_schema', 10, 8 );
    function san_add_schema( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
       return $html . get_schema( $id );
    }

    Note that get_schema() is not a defined function, the method to get the schema needs to be developed still.

    About HTML/WP — if the permalink system is used to respond with specific HTML, there’ll be something about the WP page object that indicated where to get the HTML, whether it’s a template, HTML file, PHP file, whatever, the point is there is a tie of some sort between WP pages with a permalink and the actual content.

    I’m not convinced obscuring the fact WP is involved is warranted. Yes, it can invite unwanted attention in the way of hack attacks. There is a widely held perception that WP is insecure. It’s not, the core is quite secure, the problem comes from plugins or themes with vulnerabilities, or some user issue like weak passwords or something. In any case, security through obscurity is a fallacy. I don’t see the point of going through this when you can accomplish most of it with regular WP pages. The rest can be accomplished through custom page templates. IMO, if you’re going to use WP, you may as well fully use WP ??

    Thread Starter san99

    (@san99)

    hi there again,
    I am sorry i could not get back, first off all I should say that I am so grateful for you answering my all questions here:) You are putting up with me quite nicely hehe. I am sorry I don’t understand very complex wp terms like crux but I am trying to learn it. As for the schema i Knew there were some plugins and i tried all one by one, trust me, I tried all. I was trying to understand how that one guy was adding schema within images that way, now I have finally figured out. He created gallery first for each post by adding a php file like gallery.php, he was using that loop in the single and attachment pages, then to add schema he was using custom functions with wordpress image attachment hooks, he was getting different sizes by their names in wp like full, medium,small and thumbnails:)… This kind of script does not exist anywhere it has to be developed.. my mistake was to assume that it was in wordpress.
    You are right we can accomplish anything with wp and I have been using it myself for years.The thing was to discover how to use html 5 functions/language with wordpress and now I know how,, we put custom functions in the theme it’s not something that a plugin can do. Yea these are sort of wordpress hacks done by developers only, you must have your secrets as well to make a wp site unique in structure. The standard structure works fine but it is being used by millions around the world, I know SEO also matters but I believe that any theme with unique layout also stands out the way your post ranks in SE.
    You told me that creating different image sizes and showing all in the source page is good for SEO , I want to make sure if using multiple alt is a safe approach. For instance in that script i could count one title and around 5-6 alts with each image.. it’s kind of name of image repeating within source page, do you think it is safe?

    Thanks for helping me out, you are a pro:)

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘How to add numeric digits to the attachment page url in wordpress?’ is closed to new replies.