• I’ve noticed that if you upload files and insert them into your post/page via any of the Add Media buttons none of the resulting media items or links have any custom CSS class assigned to them.

    E.g. If you attach a text file and then insert the link to it into your post, you just get the following:

    <a href="http://www.site.com/wp-content/uploads/somefile.txt">some file</a>

    Surely it must be possible to either create a plugin or edit the core files such that when you Add Media and insert it into the post it would post the link with an added CSS class attribute based on the type of media being inserted so that you could then style those uploaded file types. E.g.

    <a class="added_misc" href="http://www.site.com/wp-content/uploads/somefile.txt">some file</a>
    
    <a class="added_img" href="http://www.site.com/wp-content/uploads/somefile.txt"><img src="img.jpg"></a>

    Why? Among other things, I want to be able to automatically have CSS for two different types of links within a post — a regular web link and a separately styled file download link generated by the Add Media window.

    If anyone knows how to do this or has an idea of how it can be done, can you point me in the right direction?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter sweetangst

    (@sweetangst)

    *knees post in groin*

    Thread Starter sweetangst

    (@sweetangst)

    *punches the post in the face!*

    Thread Starter sweetangst

    (@sweetangst)

    … Well.. I guess for now I’ve just decided to set up the html/ CSS like so:

    html/php

    <div class="entry">
    <ul>
    <li>link to file</li>
    <li>link to file</li>
    </ul>
    </div>

    css

    div.entry ul li a {
       css properties here
    }

    At least this way a regular link won’t get styled the same way as one placed in a list. I’m still hoping for a better option though…

    If you’re getting HTML output in your post, it’s only a string of what class(5)=””(3)name(x) …. say 15 – 20 characters to add a class by typing it in…

    If you know how to style a class in the CSS then you’re most likely capable of writing 20 characters onto the HTML in your post…

    Then you also have CSS selectors…

    To add that functionality would be to add additional overhead for very little return…

    You can effectively style all the elements in the post without any additional classes…

    Brief example…
    .postclass ul {} – First list that exists inside the post
    .postclass ul ul {} – Child list inside the post
    .postclass ul+ul {} – Second list..

    Now bear in mind when using selectors if you were to use the above and style the second list different then the first, the third, forth, and additional lists would also be styled like the second, so you’d just do…
    .postclass ul, .postclass ul+ul+ul { css stuff }
    .postclass ul+ul { second list specific css }

    All i’m saying is, there’s no need for additional classes when CSS can selectively grab any element you need to style already… πŸ˜‰

    Thread Starter sweetangst

    (@sweetangst)

    I appreciate the reply but I think perhaps I was unclear.

    This site isn’t for me. Yes, I know how to add the extra CSS class to the uploaded media via HTML view — Since the site isn’t for me and may include a lot of download links, I’d like to just avoid that.

    Using the download links in a list format is just a work around I came up with for the time being — I would much rather have wordpress automatically add the CSS class to the link it generates after uploading a media file.

    I assume some core file in wordpress is telling itself to print all the required tags (e.g. <a href="blah.jpg">Picture Link</a>) to produce and insert that link for XYZ uploaded media.

    I want to edit the core (?) file such that whenever it is asked to insert a link to an uploaded media file (via the Add Media window), it automatically adds a class identifying it as a link to an uploaded file (as opposed to a regular weblink).

    <a class="uploaded_file" href="blah.jpg" >Picture Link</a>
    
    <a class="uploaded_file" href="wp-content/uploads/somefile.txt">some file</a>
    
    <a class="uploaded_file" href="wp-content/uploads/somezipped.zip">some zipped file</a>

    If you can tell WordPress to filter how it prints links to Media vs Regular Links, then the class above would be different than just a regular weblink either entered via the Visual Editor or via the “Add Link” button. E.g.

    <a class="regular_link" href="http://www.site.com">Regular Link</a>

    I see what you’re saying now, sounds like a neat idea…

    I know this is not ideal, but you can be really selective with the CSS…

    .entry a[href^="http://"] {
      css stuff..
    }
    
    .entry a[href*="wp-content/uploads/"][href$=".jpg"],
    .entry a[href*="wp-content/uploads/"][href$=".gif"],
    .entry a[href*="wp-content/uploads/"][href$=".png"] {
      css stuff..
    }

    There’s also the option of using jQuery selectors to do similar to the above except adding a class on page load…

    Just offering alternatives… πŸ˜‰

    Although i would agree if these were added automatically it would be a much cleaner solution…

    I’ll have a dig and see if i can find which file handles the code insertion for you…

    Thread Starter sweetangst

    (@sweetangst)

    Alternatives are always good πŸ™‚

    Had a nice look but couldn’t immediately see anything related..

    I’m pretty sure the JS handles it via an onclick event (ie when you click Add to Post), but JS really isn’t within the scope of my knowledge..

    CSS is straight-forward enough though… πŸ™‚

    RE: CSS selectors above..
    [attribute] – has this attribute
    [attribute$=""] – has this attribute and ends with
    [attribute^=""] – has this attribute and starts with
    [attribute*=""] – has this attribute and contains

    So above i looked for an “a” element with a [href] attribute containing wp-content/uploads and ending with an image extension… that is a child element of the entry class..

    Thread Starter sweetangst

    (@sweetangst)

    I think that CSS is a little out of my league until I have time to catch up with how much it’s changed…

    Thanks for all your advice… now I just have to go do a bunch more reading to figure out how to use it πŸ™‚

    Ok a little more a break down…

    Let’s say we had 5 links, in a list, like so…

    <ul class="example">
    	<li><a href="http://wordpress.org/support/topic/290029?replies=10">This thread</a></li>
    	<li><a href="http://www.google.com">Google</a></li>
    	<li><a href="http://www.wordpress.org">Wordpress</a></li>
    	<li><a href="http://www.t31os.co.uk">My site</a></li>
    	<li><a href="http://www.ranoutofideas.com">Some example link</a></li>
    </ul>

    And now imagine we’d like to style the google link in that list…

    First we’d target the list, because it has a class… so..
    ul.example
    then the list elements, so now..
    ul.example li
    Next find the link elements….
    ul.example li a

    Right now that targets all link elements.. so out comes the first selector.

    ul.example li a[href]

    So far that just selects any <a> with a “href” attribute (so pretty much any <a> element… ok let’s get more selective…
    ul.example li a[href^=""]
    A link that “starts with…
    ul.example li a[href^="http://"]
    Still not much help, nearly all links start with http://
    ul.example li a[href^="http://www.google"]

    It now matches any link element that has a HREF attribute that starts with.. http://www.google

    Hopefully the previous post makes a little more sense now… πŸ˜‰

    Thread Starter sweetangst

    (@sweetangst)

    Yeah, I get it now.. I just think it might be over complicating it.

    I was thinking about just editing maybe wp-includes/post-template.php and adding the classes I want in there. Haven’t had time to try it yet though.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Add custom CSS classes to Add Media uploads?’ is closed to new replies.