Skip to:
Content
Pages
Categories
Search
Top
Bottom

hiding activity form for all users except admin


  • exp3rts
    Participant

    @exp3rts

    hello,

    im a none developer and would like to ask if there is a code that you could provide for me to put it into the plugin code snippet (php code) which would hide the activity form (on all sites) from user but stay visible for admins only.

    thanks a lot

Viewing 9 replies - 1 through 9 (of 9 total)

  • exp3rts
    Participant

    @exp3rts

    ps: what i want to achieve is that only admins can write activity posts.


    exp3rts
    Participant

    @exp3rts

    so you just made an account to post your plumbing services which has nothing to do with my problem?


    ericwillson
    Participant

    @ericwillson

    To achieve the functionality you described, you can add the following PHP code snippet to your WordPress plugin or theme’s functions.php file. This code will hide the activity form from all users except admins:

    php
    Copy code
    function hide_activity_form_for_non_admins() {
    if ( ! current_user_can( ‘administrator’ ) ) {
    ?>
    <style type=”text/css”>
    .activity-form-class {
    display: none;
    }
    </style>
    <?php
    }
    }
    add_action( ‘admin_head’, ‘hide_activity_form_for_non_admins’ );

    In this code snippet:

    current_user_can(‘administrator’) checks if the current user is an administrator.
    If the current user is not an administrator, it adds a <style> block to the <head> of the admin pages, setting the CSS property display: none; for the element with the class activity-form-class. Make sure to replace activity-form-class with the actual class name or ID of the activity form element in your HTML markup.
    EXP3RTS ensures you have a backup of your site or are comfortable with making changes to your theme/plugin files before adding this code. Additionally, modify the class name in the code to match the actual class or ID of your activity form for it to work correctly.
    I HOPE THIS IS BENEFICIAL FOR YOU


    exp3rts
    Participant

    @exp3rts

    thanks for this suggestion. just tested it and got an internal server error (500).
    wordpress says now it has a critical error.

    i also rather would like to disable the activity form for users instead of just not showing it, since you can easily bypass display:none in the inspection tools.

    thanks


    ericwillson
    Participant

    @ericwillson

    opps ] Let me provide you with an alternative approach that disables the activity form for users, making it more secure. You can achieve this by disabling the form functionality entirely. Here how you can modify the code to accomplish thi

    php
    Copy code
    function disable_activity_form_for_non_admins() {
    if ( ! current_user_can( ‘administrator’ ) ) {
    remove_action( ‘init’, ‘function_that_creates_activity_form’ );
    }
    }
    add_action( ‘init’, ‘disable_activity_form_for_non_admins’ );

    In this code snippet, replace ‘function_that_creates_activity_form’ with the actual function name or hook that creates the activity form. This modification disables the activity form functionality for non-administrative users by removing the action that creates the form during the init hook.

    This way the form wont be available in the HTML markup, making it more secure compared to just hiding it with CSS. Remember to place this code in your theme’s functions.php file or in a custom plugin file.

    Again make sure to back up your site before making any changes, and test the modified code in a safe environment first to ensure it works as expected. If you encounter any issues or have further questions, feel free to ask!


    exp3rts
    Participant

    @exp3rts

    ah yes great, this comes closer to my goal.

    i guess i have to replace “function_that_creates_activity_form” with the real function name. could you please give me a hint on where i can find this specific function name?

    thanks a lot


    ericwillson
    Participant

    @ericwillson

    To find the specific function responsibl for creating the activity form
    Check the theme’s functions.php file Look for a related function in your active theme functions.php file
    Inspect plugin files If the form is part of a plugin, search for relevnt functions within the plugin’s PHP files.
    Search documentation Consult the plugin or theme documentation for information on activity form functions
    Use a code editor/IDE Utilize search feature in your code editor/IDE to find keywords related to the activity form.
    Contact the develper If needed, reach out to the theme or plugin developer for specific function details.
    Once you identify the function, replace ‘function_that_creates_activity_form’ in the provided code snippet with the actual function name to disable the activity form for non-admin users.


    exp3rts
    Participant

    @exp3rts

    oh ok, so this is theme related. i thought its “just” a buddypress function(name).

    thanks a lot


    ericwillson
    Participant

    @ericwillson

    You r welcome If the activity form is related to BuddyPress you might want to check BuddyPress specific functions or hooks in your theme or plugin files. BuddyPress indeed has its own set of functions for handling activities and forms.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar