Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom class to fields? #141

Closed
anointed opened this issue Jul 17, 2012 · 7 comments
Closed

Adding custom class to fields? #141

anointed opened this issue Jul 17, 2012 · 7 comments

Comments

@anointed
Copy link

I needed to have field specific classes in order to target them via jQuery. Now I know I can add a class manually during creation of the field but I'm lazy.

One idea was to simply add this line to the meta-box.php class line 259

$class = $this->add_cssclass( $field[ 'id' ], $class );

Obviously I don't want to hack the core file, and I did see a filter hook in there, but I just could not figure out how to write a filter to add the field id as a class to all the fields.

If the filter can be used for this, can someone copy a working snippet in here for me to use?
*I'm not even sure if the filter I was trying would work, hence the question.

@rilwis
Copy link
Member

rilwis commented Jul 18, 2012

You can use rwmb_begin_html to filter the begin HTML of all fields. Something like this:

add_filter( 'rwmb_begin_html', 'add_my_class' );

function add_my_class( $begin, $field, $meta )
{
     // Copy the content of function begin_html() and change it as you like
}

@anointed
Copy link
Author

I'm sorry to say, but I am stuck:

Here is the snippet from what you are suggesting:

add_filter( 'rwmb_begin_html', 'add_my_class' );

function add_my_class( $begin, $field, $meta  ) {

            $class = 'rwmb-label';

            if ( ! empty( $field['class'] ) )
                $class = self::add_cssclass( $field['class'], $class );

            if ( empty( $field['name'] ) )
                return '
'; $html = << {$field['name']}
HTML; return $html; }

When I do that I get this error:
Warning: Missing argument 2 for add_my_class()

I am simply trying to change 'div class="rwmb-field"' to div class="rwmb-field-id" (id is the id of the field being shown)

Sorry for the newb question, but I spent the entire evening,morning trying every combination that I could think of, and nothing is working.

Can you show me where I am going wrong?

@anointed
Copy link
Author

sorry about the formatting above, can't figure out why git is not closing off my code snippet the right way.....

@rilwis
Copy link
Member

rilwis commented Jul 19, 2012

Please try: add_filter( 'rwmb_begin_html', 'add_my_class', 10, 2 );

@anointed
Copy link
Author

Btw, it needs to be 10, 3 not 10,2...

Here's what I ended up with. It doesn't do what I want "remove the rwmb-field" and replace it with "rwmb-field field-id", as I can't seem to figure out how to modify that wrapper div. However it does work for me to get by for now.


function add_my_class( $begin, $field, $meta ) {
            $begin = <<
HTML;
            $end = '';

            $class = 'rwmb-label';

            if ( ! empty( $field['class'] ) )
                $class = self::add_cssclass( $field['class'], $class );

            if ( empty( $field['name'] ) )
                return '
'; $html = << {$field['name']}
HTML; return $begin .$html .$end; }

Probably a lousy way of doing it, but I'm still learning. Thnx for the pointer

@anointed
Copy link
Author

Damn it's hard to get github to take my code without screwing it up... arghh.

I am wrapping my code with (pre)(code) and ending it with (/code)(/pre) obviously changing ( for a <
Is that the wrong way?

https://gist.github.com/3140691 works better

@rilwis
Copy link
Member

rilwis commented Jul 22, 2012

Hi, github uses Markdown syntax to parse comments, you can click the link to see the details.

Looks like your code is good, so I'll close this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants