Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Any wordpress/php developer here ?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Any wordpress/php developer here ?

Hi,

I need some help with a wordpress plugin which adds a meta box on the post page (in the backend) which I need to be turned off/on based on the category selected. Not too much work for someone who knows his stuff.

Anyone here which can do it or any recommendations ?

Regards

Comments

  • agentmishraagentmishra Member, Host Rep

    a plugin already exists...

  • noamannoaman Member
    edited February 2017

    It can be done using jQuery

    Just attach to the event onChange of category selection to toggle display of the specific meta box...

  • JunklessJunkless Member
    edited February 2017

    @agentmishra said:
    a plugin already exists...

    @noaman said:
    It can be done using jQuery

    Just attach to the event onChange of category selection to toggle display of the specific meta box...

    @srvrpro said:
    Use https://www.advancedcustomfields.com/

    Thanks guys for the response. I guess I did not phrase the requirements correctly. Let me go again :

    What I have - A plugin is installed which shows a meta box at the post page (in the backend) with some options to select, which affect the output of the post on the front end.

    What I need - To show the above meta box only for some selected category posts. So if a post is from a certain category, only then the meta box should be active.

    Hope it clears it up a bit.

  • varunchopravarunchopra Member
    edited February 2017

    You can only change it for custom post types, not post categories. See. https://codex.wordpress.org/Post_Types

    You can disable the output on the front end though (according to post category).

  • @Junkless said:

    @agentmishra said:
    a plugin already exists...

    @noaman said:
    It can be done using jQuery

    Just attach to the event onChange of category selection to toggle display of the specific meta box...

    @srvrpro said:
    Use https://www.advancedcustomfields.com/

    Thanks guys for the response. I guess I did not phrase the requirements correctly. Let me go again :

    What I have - A plugin is installed which shows a meta box at the post page (in the backend) with some options to select, which affect the output of the post on the front end.

    What I need - To show the above meta box only for some selected category posts. So if a post is from a certain category, only then the meta box should be active.

    Hope it clears it up a bit.

    It is possible with ACF.

  • Ohh. But I was looking to do it using code, instead of a plugin, if possible.

  • You can do it easily
    Example :

    <?php if ( in_category( 4 ) ) : ?>


    • <?php the_field('event_date'); ?>
    • <?php the_field('event_time'); ?>
    • <?php the_field('booking_fee'); ?>
    • <?php the_field('event_location'); ?>


    <?php endif; ?>

    http://wordpress.stackexchange.com/questions/170364/if-category-then-echo-a-custom-fields

    Thanked by 1Junkless
  • F*ck acf. Use pods.io if anything. And if your modifying an existing plugin and need assistance, ping me at https://codeable.io/developers/derrick-hammer/?ref=rvtGZ :).

    Peace.

    Thanked by 1Junkless
  • @Junkless said:

    @agentmishra said:
    a plugin already exists...

    @noaman said:
    It can be done using jQuery

    Just attach to the event onChange of category selection to toggle display of the specific meta box...

    @srvrpro said:
    Use https://www.advancedcustomfields.com/

    Thanks guys for the response. I guess I did not phrase the requirements correctly. Let me go again :

    What I have - A plugin is installed which shows a meta box at the post page (in the backend) with some options to select, which affect the output of the post on the front end.

    What I need - To show the above meta box only for some selected category posts. So if a post is from a certain category, only then the meta box should be active.

    Hope it clears it up a bit.

    Yes it would be a little difficult

    You need to get the post id in the action where it is attached or you can do it where you add the html

    and see if you get post id

    then check its categories and then return false if no content is not found

    Only hiccup would be when there is a new post

    as there shouldnt be an ID for a new post

    My 2 cents

    Thanked by 1Junkless
  • You don't need the post id because its automatically shows the right category in single.php file

  • @Jorbox said:
    You don't need the post id because its automatically shows the right category in single.php file

    What?

    Single.php

    He is talking about the backend

    Thanked by 1Junkless
  • FalzoFalzo Member
    edited February 2017

    use developer console of your browser to identify some specific name/id/class of the custom field element. search for it in the plugin directory via shell with find and grep to identify the file which generates this. go and edit some kind of if-clause around it.

    that's most probably bad practice after all, as there are several drawbacks:

    • any update of the plugin later on may overwrite your edits, so you would want to lock it from updating, which may cause security concerns or incompatibilities with upcoming wordpress-versions

    • especially categories are subject to create/change/remove in wordpress even by other users, which may force you to extend or change your if-clause everytime.

    are you sure you want to get rid of the custom field in the backend? or do you more likely want to get rid of the output it produces (only for certain categories)

  • @Falzo said:
    use developer console of your browser to identify some specific name/id/class of the custom field element. search for it in the plugin directory via shell with find and grep to identify the file which generates this. go and edit some kind of if-clause around it.

    that's most probably bad practice after all, as there are several drawbacks:

    • any update of the plugin later on may overwrite your edits, so you would want to lock it from updating, which may cause security concerns or incompatibilities with upcoming wordpress-versions

    • especially categories are subject to create/change/remove in wordpress even by other users, which may force you to extend or change your if-clause everytime.

    are you sure you want to get rid of the custom field in the backend? or do you more likely want to get rid of the output it produces (only for certain categories)

    I have modified my approach since then. The custom field is basically a selection box (enable or disable), so just trying to auto select enable for certain categories now, instead of getting rid of the box.

    Also, the category will stay static, no changes without authorization. And the plugin is a customized one, so will not be updated without inheriting these changes.

    @noaman seems to get what is required. Hoping he can help.

  • @Junkless said:

    I have modified my approach since then. The custom field is basically a selection box (enable or disable), so just trying to auto select enable for certain categories now, instead of getting rid of the box.

    Also, the category will stay static, no changes without authorization. And the plugin is a customized one, so will not be updated without inheriting these changes.

    first one seems reasonable and the less invasive approach, which also should easily if categories ever change.

    also if the plugin is custom-made, then for sure further changes shouldn't hurt much.

    biggest problem for doing such things in the backend probably will be related to the possibility to change the category in the same editor page, which contains your selection box. so when switching the category, you may need to change the selection of that box. also what about multiple categories per post? ;-)

    after all not as easy as it seems I'd say.

  • @noaman said:

    @Jorbox said:
    You don't need the post id because its automatically shows the right category in single.php file

    What?

    Single.php

    He is talking about the backend

    "which affect the output of the post on the front end."
    Please read everything he says

  • @Falzo said:

    @Junkless said:

    I have modified my approach since then. The custom field is basically a selection box (enable or disable), so just trying to auto select enable for certain categories now, instead of getting rid of the box.

    Also, the category will stay static, no changes without authorization. And the plugin is a customized one, so will not be updated without inheriting these changes.

    first one seems reasonable and the less invasive approach, which also should easily if categories ever change.

    also if the plugin is custom-made, then for sure further changes shouldn't hurt much.

    biggest problem for doing such things in the backend probably will be related to the possibility to change the category in the same editor page, which contains your selection box. so when switching the category, you may need to change the selection of that box. also what about multiple categories per post? ;-)

    after all not as easy as it seems I'd say.

    I totally agree with everything you said. though it is not actually required, just the client wants it that way. Sigh

  • @Jorbox said:

    "which affect the output of the post on the front end."

    valid point.

    might be easier to do, just ignore the selection box at all and hard code the choice within the template. for posts in multiple categories depend on the primary anyways.
    unaffected by category-changings as you wont need to use js or something to change the selection box accordingly while in the editor...

    depends if single post is the only place which is affected by the choice ;-)

    also to avoid misunderstandings one should remove the whole custom field in the backend though.

    Thanked by 1Jorbox
  • @Falzo said:

    @Jorbox said:

    "which affect the output of the post on the front end."

    valid point.

    might be easier to do, just ignore the selection box at all and hard code the choice within the template. for posts in multiple categories depend on the primary anyways.
    unaffected by category-changings as you wont need to use js or something to change the selection box accordingly while in the editor...

    depends if single post is the only place which is affected by the choice ;-)

    also to avoid misunderstandings one should remove the whole custom field in the backend though.

    Yes Right

    less coding and this seems to be a better way of doing it

    @Junkless said:

    @Falzo said:

    @Junkless said:

    I have modified my approach since then. The custom field is basically a selection box (enable or disable), so just trying to auto select enable for certain categories now, instead of getting rid of the box.

    Also, the category will stay static, no changes without authorization. And the plugin is a customized one, so will not be updated without inheriting these changes.

    first one seems reasonable and the less invasive approach, which also should easily if categories ever change.

    also if the plugin is custom-made, then for sure further changes shouldn't hurt much.

    biggest problem for doing such things in the backend probably will be related to the possibility to change the category in the same editor page, which contains your selection box. so when switching the category, you may need to change the selection of that box. also what about multiple categories per post? ;-)

    after all not as easy as it seems I'd say.

    I totally agree with everything you said. though it is not actually required, just the client wants it that way. Sigh

    Part of your job is to tell the client not to do the work but do the work right way...and convince him...just my opionion

    Because thats what I do and they usually understand :-)

  • @Junkless if you still need help I might be able to help you but not before the end of next week. I am not a fiverr though ;-)

    Thanked by 1Junkless
  • @noaman said:

    Part of your job is to tell the client not to do the work but do the work right way...and convince him...just my opionion

    Because thats what I do and they usually understand :-)

    Totally agree. That's what I did and thankfully, it made sense to them as well.

    @Falzo said:
    @Junkless if you still need help I might be able to help you but not before the end of next week. I am not a fiverr though ;-)

    Thanks man, but it's sorted for now. I already owe you a drink for helping me setting up RAID one time on that OVH server.

    Thanked by 1Falzo
  • I understand it is fix. Let me know if you need any assistance.

Sign In or Register to comment.