My goal is obtain "Edit" button like this:

<a class="post-edit-link btn btn-default" href="{$url}">Edit this</a>

The first attempt I was trying adopt was adding before and after params:

edit_post_link( __('Edit this'), '<span class="btn btn-default">', '</span>');

But the result wasn't satisfying:

<span class="btn btn-default">
    <a class="post-edit-link" href="{$url}">Edit this</a>

First of all, it's redundant: I must still changing CSS to customize Edit button and there is nonsense to get more elements only to add CSS class.

Second attempt became the solution. I used apply filter to replace classes in string:

function dbj_edit_post_link($output) {
    $output = str_replace('class="post-edit-link"', 'class="post-edit-link btn btn-default"', $output);
    return $output;
add_filter('edit_post_link', 'dbj_edit_post_link');

And yes, this is what I want, but there are really no more less clunky solution to add class to "Edit" button?

  • Any particular reason why you can't just style the .edit-post-link class that's already there? That class is unique in WordPress core (i.e. it's not used by anything other than the edit_post_link() function. If you're using SASS or LESS you could define the .btn stuff as a mixin, if it's the unnecessary repetition that bothers you… – William Turrell Apr 13 '15 at 18:15
  • Yes, the particular reason is that I'm using Bootstrap. Indeed, there is .edit-post-link, but which I mentioned: this solution make additional need to change CSS. Why not using something which already exists only by adding class, without any manipulation of CSS framework core? I think this is the purpose of using framework. – Dawid Janiga Apr 13 '15 at 18:44
  • 1
    Well in that case, the filter in your question is the only solution. I agree the function isn't ideal but WordPress is an open source project that relies on people's contributions so you'd be very welcome to submit a patch if you can improve it – see make.wordpress.org/core for details. – William Turrell Apr 13 '15 at 19:11
  • Thank you for your answer, I will try find appropriate solution to solve this issue in edit_post_link function and later send it to WordPress community. – Dawid Janiga Apr 13 '15 at 20:54

Assuming you are reading this answer in the future (December 2015 or later).

Starting from WordPress 4.4.0, The $class argument was added. So:

edit_post_link( __('Edit'), '', '', 0, 'post-edit-link btn btn-default' );

See edit_post_link documentation.

| improve this answer | |
  • Thanks for this, could not find anything on this feature in the official docs. – Brigante Jan 17 '16 at 12:25
  • @Brigante I added a link to the docs. – Nabil Kadimi Jan 17 '16 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.