Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] xProfile field being created with every reload

  • @hardeepasrani

    Participant

    I’m using following snippet to create xProfile fields in a custom plugin:

    `
    add_action(‘bp_init’, ‘field_xprofile_twitter’);

    function field_xprofile_twitter()
    {
    global $wpdb;
    xprofile_insert_field(
    array (
    field_group_id => 1,
    name => ‘WWWYKI’,
    can_delete => false, // Doesn’t work *
    field_order => 1,
    is_required => false,
    description => ‘Woo Woo Woo You Know it, bro!’,
    type => ‘textbox’
    )
    );
    }
    `

    But it’s creating a new field every time the script runs. Is there a way to prevent that from happening?

Viewing 8 replies - 1 through 8 (of 8 total)
  • @r-a-y

    Keymaster

    You shouldn’t be loading your field_xprofile_twitter() function on ‘bp_init’. Doing this creates your field on every page load.

    Why don’t you just create your field once in the WP admin dashboard under “Users > Profile Fields”?

    @hardeepasrani

    Participant

    Because I’m trying to create a plugin out of it.

    @danbp

    Participant

    Hi @hardeepasrani,

    xProfile field manipulation is a such common question on this forum. ๐Ÿ™„ Did you searched before asking ?

    But codex first !

    Creating a Plugin


    other reference

    Here a topic where you can find how this works and can be done.
    https://buddypress.org/support/topic/how-to-add-fields-from-xprofile-fields-groups-created-by-me/

    @hardeepasrani

    Participant

    That’s not what I’m looking for.

    @shanebp

    Moderator

    For a plugin, instead of bp_init, use:
    https://codex.wordpress.org/Function_Reference/register_activation_hook

    note: I’m not sure BP is loaded before that hook is called.

    Or you could use bp_init, but in your function, check for the existence of the xprofile field before you create it.

    btw – you don’t need the global in that function.

    @hardeepasrani

    Participant

    @shanebp

    Okay, it worked!!! That’s a lot!!

    One more thing – can you please tell me what if it’s in the functions.php file, do I need to use the bp_init then or something else?

    @shanebp

    Moderator

    >Okay, it worked!!!

    You mean using the register_activation_hook ?
    btw – you should still check to see if the field exists before you create it – to avoid duplications if they deactivate / reactivate the plugin.

    Or delete the field by using this hook:
    https://codex.wordpress.org/Function_Reference/register_deactivation_hook

    >can you please tell me what if itโ€™s in the functions.php file, do I need to use the bp_init then or something else?

    Perhaps bp_loaded is more appropriate, you’ll have to try it and find out.

    BP related functions are usually placed in bp-custom.php

    @hardeepasrani

    Participant

    Amazing!!

    Where’s the mark as resolved or close the topic option?

    Or maybe adding a tag would do that? Let me try…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] xProfile field being created with every reload’ is closed to new replies.
Skip to toolbar