Forum Replies Created

Viewing 15 replies - 121 through 135 (of 718 total)
  • Plugin Author donmik

    (@atallos)

    Have you read the FAQ?

    Plugin Author donmik

    (@atallos)

    If your theme is overriding this files (register.php and edit.php) then it’s probably due to this. Your theme is probably missing the hooks responsible of displaying the custom fields.

    Read the FAQ here

    Plugin Author donmik

    (@atallos)

    Your theme is probably missing some hooks. Compare your files with buddypress files located inside buddypress/bp-themes/bp-default/registration/register.php.

    Plugin Author donmik

    (@atallos)

    This code is not from my plugin. Have you added it to my plugin? First I can say

    <?php
    class Bxcft_Field_Type_Phone {

    }

    function formatPhoneNumber($phoneNumber) {`

    You are closing brace after “class Bxcft_Field_Type_Phone {” and all the code is outside so this is wrong.

    Plugin Author donmik

    (@atallos)

    Thanks again for checking my code and ask questions.

    We cannot use parent calls because buddypress parent method overrides the value of $richtext_enabled. So our code will be useless.

    But, you’re right, my first version of this plugin is not the best. Overriding and copying buddypress functions is not the best way to achieve this. Especially when we have “bp_xprofile_is_richtext_enabled_for_field” filter ready to use.

    So, I’ve changed my code. Now I’m not overriding edit_field_html or admin_field_html. What I’m doing is use the previous filter.

    In the main file, I’ve added this line to “init” method:

    add_filter( 'bp_xprofile_is_richtext_enabled_for_field', array($this, 'bpxct_disable_richtext'), 10, 2 );

    And at the end of the class, I’ve added the new method to disable richtext:

    public function bpxct_disable_richtext($enabled, $field_id) {
                // Init $field object with $field_id.
                $field = new BP_XProfile_Field($field_id);
                if (!$field) {
                    return $enabled;
                }
    
                // I'm looking inside the options to check if the checkbox "disabled_richtext
                // for this field is checked.
                $options = $field->get_children();
                foreach ($options as $option) {
                    if ($option->name == 'disable_richtext') {
                        // Found it, so $richtext_enabled should be false
                        $enabled = false;
                        // Stop looking for it, just in case.
                        break;
                    }
                }
    
                return $enabled;
            }

    Of course, I’ve removed edit_field_html and admin_field_html methods in our custom textarea class.

    I believe this is the better way to do this. You can download the new version in github.

    Plugin Author donmik

    (@atallos)

    I have not tried really but I don’t think we have any issue with this.

    Plugin Author donmik

    (@atallos)

    Check the FAQ to modify the size of th file.

    Plugin Author donmik

    (@atallos)

    You can use everywhere you want. What you will get with this code is an img tag:

    <img src="path of your image" />

    Plugin Author donmik

    (@atallos)

    I’ve uploaded the plugin I created to github. Feel free to use it.

    I will probably writing an article about this in my blog tomorry so if you are interested check my blog tomorrow 🙂

    Plugin Author donmik

    (@atallos)

    I’ve just been playing around with this, so I have the code working. I’m going to explain a little bit how this is working.

    I’ve copied the buddypress class of Textarea inside my plugin and created a class named “Bxcft_Field_Type_Textarea”. This class will extend the original class “BP_Xprofile_Field_Type_Textarea”.

    1. Constructor.
    We need to support options:
    $this->supports_options = true;

    This is necessary to display the checkbox to disabled richtext.

    2. Override “admin_new_field_html” method.

    This method is responsible of display the checkbox form. So with this code:

    public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '')
            {
                $type = array_search( get_class( $this ), bp_xprofile_get_field_types() );
                if ( false === $type ) {
                    return;
                }
    
                $class            = $current_field->type != $type ? 'display: none;' : '';
                $current_type_obj = bp_xprofile_create_field_type( $type );
    
                $options = $current_field->get_children( true );
                if ( ! $options ) {
                    $options = array();
                    $i       = 1;
                    while ( isset( $_POST[$type . '_option'][$i] ) ) {
                        $is_default_option = true;
    
                        $options[] = (object) array(
                            'id'                => -1,
                            'is_default_option' => $is_default_option,
                            'name'              => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),
                        );
    
                        ++$i;
                    }
    
                    if ( ! $options ) {
                        $options[] = (object) array(
                            'id'                => -1,
                            'is_default_option' => false,
                            'name'              => '',
                        );
                    }
                }
            ?>
                <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;">
                    <h3><?php esc_html_e( 'Disable richtext:', 'bxcft' ); ?></h3>
                    <div class="inside">
                        <p>
                            <?php _e('Check this if you want to disable richtext for this field:', 'bxcft'); ?>
                            <input type="hidden" name="<?php echo esc_attr( "{$type}_option[0]" ); ?>" id="<?php echo esc_attr( "{$type}_option0" ); ?>" value="enable_richtext" />
                            <input type="checkbox" name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" id="<?php echo esc_attr( "{$type}_option1" ); ?>" value="disable_richtext"
                                   <?php if ($options[0]->name == 'disable_richtext') : ?>checked="checked"<?php endif; ?>/>
                        </p>
                    </div>
                </div>
            <?php
            }

    We will display a checkbox to disable richtext. With this code, the default behavior is enable richtext. User has to disable it checking the checkbox. If you want default behavior to be richtext disable. You just have to change options “enable_richtext” by “disable_richtext”.

    3. Replace code inside “admin_field_html”

    This method is responsible of display the field inside the list of profile fields in wordpress admin. Just replace this line:

    $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field();

    With this:

    global $field;
            $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field();
            $options = $field->get_children();
            foreach ($options as $option) {
                if ($option->name == 'disable_richtext') {
                    $richtext_enabled = false;
                    break;
                }
            }

    This code is looking for the “disable_richtext” option of $field. If it exists, it will disable the richtext ($richtext_enabled = false;).

    4. Replace code in “edit_field_html”

    This method display the field in wordpress frontend edit profile form.

    Replace this:
    $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field();

    With:

    global $field;
    $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field();
            $options = $field->get_children();
            foreach ($options as $option) {
                if ($option->name == 'disable_richtext') {
                    $richtext_enabled = false;
                    break;
                }
            }

    This code do the same thing.

    Now the admin.js changes:

    You have to implement the same behavior I’ve done for birthdate field. If you look at the “admin_new_field_html”, I’ve created two input fields. One is hidden and is “enable_richtext”. The other one is checkbox and is “disable_richtext”. When you check “disable_richtext” and click on update, we need to remove the hidden field before saving data because buddypress will save all the field options so if you send with the form the hidden option buddypress will save: “enable_richtext” AND “disable_richtext”, but we only want one of them.

    So you can do this way:

    1. Modify my original “bxcft_remove_empty_checkbox”, replace with:

    function bxcft_remove_empty_checkbox($, type) {
        if ($('#' + type + '_option1').is(':checked')) {
            $('#' + type + '_option0').remove();
        }
    }

    This function can be used now for any type give “type” param. So I can reuse it for our new textarea field.

    2. Add below:

    else if ($('select#fieldtype').val() == 'birthdate') {
                bxcft_remove_empty_checkbox($, 'birthdate');
            }

    This code:

    else if ($('select#fieldtype').val() == 'textarea') {
                bxcft_remove_empty_checkbox($, 'textarea');
            }

    The last thing we have to do is add the new field and register new type of field. You have to do it in “bp-xprofile-custom-fields-type.php”.

    1. In init() method, add this require_once:

    require_once( 'classes/Bxcft_Field_Type_Textarea.php' );

    2. In bxcft_get_field_types method, add the new type to the array $new_fields:

    'textarea' => 'Bxcft_Field_Type_Textarea',

    All done! This should work.

    The last problem you have to think is to do your own plugin with this… because if you still want to use my plugin. Your code will be overriden so I recommend you to create your own plugin.

    It’s not hard, you can copy and paste my plugin and delete all you don’t need:

    • Remove all classes (not yours) you are not using from classes folder.
    • Remove from js folder, all files (not admin.js).
    • Inside bp-xprofile-custom-fields-type.php, Remove all methods but:
      – construct
      – init
      – admin_init
      – admin_notices
      – bxcft_get_field_types
      – update
      – activate
      – deactivate

      You have to remove from construct all the add_filter or add_action you don’t need (all methods you have removed). From init, remove all classes you don’t require anymore. Remove the javascript you don’t need to enqueue…

    • In admin.js, remove all code but the function and code for your new type of field.
    • Replace everywhere you see bxcft with your own plugin name or plugin abbreviation.

    I hope you will be able to achieve what you want with this explanation. If you have any problem, I can send you the code. Feel free to ask.

    Plugin Author donmik

    (@atallos)

    Hi,

    The support thread you are linking was for display the image in the member header. So I recommend him to use:

    bp_member_profile_data('field=Image');

    This code will return the contents of a field named “Image” as an img tag.

    If you only want the url of your image in any page of your theme, you can use:

    `echo bp_get_profile_field_data(array(
    ‘field’ => ‘Image’,
    ‘user_id’ => bp_loggedin_user_id()
    ));’

    This code will show the image from field named “Image” of the logged in user anywhere in your page. This will display an img tag, so if you only want the url you have to extract the uri from the img tag if you want.

    You can use a filter to modify the way this type of field is displayed and return only the uri. Check the FAQ.

    Plugin Author donmik

    (@atallos)

    Hi,

    I believe you made a lot of work but it was an easier solution. You can deactivate richtext if you want using the following filter: “bp_xprofile_is_richtext_enabled_for_field”.

    So if you want to disable richtext for all fields, just copy and paste the following code inside your functions.php of your theme:

    function bp_disable_richtext($enabled, $field_id) {
    	$enabled = false;
    	return $enabled;
    }
    add_filter('bp_xprofile_is_richtext_enabled_for_field', 'bp_disable_richtext', 10, 2);

    If you want to disable only for one field, check against $field_id.

    In your case, your new field is not working because you have copied datepicker and datepicker is an input. If you want to create your own textarea I recommend you to copy buddypress textarea original class located in /wp-content/plugins/buddypress/bp-xprofile/classes/.

    You can copy and paste class-bp-xprofile-field-type-textarea.php and disable richtext if you want but I recommend you strongly to use the filter.

    Thanks!

    Plugin Author donmik

    (@atallos)

    Sorry, but I didn’t find anything like what you need.

    Plugin Author donmik

    (@atallos)

    Plugin Author donmik

    (@atallos)

    Sorry, this bug is solved in 2.4.1. Need more coffee! 🙂

Viewing 15 replies - 121 through 135 (of 718 total)