Skip to:
Content
Pages
Categories
Search
Top
Bottom

Need help converting PHP to run within my functions.php file.


  • Mark
    Participant

    @markob17

    Hi Everybody,

    Hoping I can get one of you experts to chime in on this for me. I’m a newbie to coding and can’t figure out how for the life of me how to convert some PHP code to run within the functions.php file.

    I want to plug it into my template via an action instead of copying and modifying yet another template file in my child theme.

    The following code works fine (albeit probably sloppy coding, took me hours to figure out) in my template file but I don’t understand how to get it working in functions.php.

    Here’s the working code snippet I’d like to convert to work in functions.php:

    
    <?php if ( bp_is_member() ) {
    ?> 
    <div class="member_avatar">
    <?php 
    global $bp;
    $args = array( 'field' => 'Gender', 'user_id' => bp_loggedin_user_id() );
    $gender = bp_get_profile_field_data($args);
    ?>
    
    <?php
    if ( $gender == "Female" ) {
    ?>
    
    <?php 
    $user_ids = my_custom_ids( 'gender', 'male' );
    if ( bp_has_members( array( 'type' => 'random', 'max' => '6', 'include' => $user_ids ) ) ) : ?>      
                        <?php while ( bp_members() ) : bp_the_member(); ?>                      
                            <div class="profileview"><a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&width=125&height=125') ?></a></div>
                        <?php endwhile; ?>
    <?php endif; ?>  
    
    <?php
    }
    ?>
    
    <?php
    if ( $gender == "Male" ) {
    ?>
    
    <?php 
    $user_ids = my_custom_ids( 'gender', 'female' );
    if ( bp_has_members( array( 'type' => 'random', 'max' => '6', 'include' => $user_ids ) ) ) : ?>      
                        <?php while ( bp_members() ) : bp_the_member(); ?>                      
                            <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&width=125&height=125') ?></a>
                        <?php endwhile; ?>
    <?php endif; ?>  
    
    <?php
    }
    ?>
    
    </div>
    
    <?php
    }
    ?>
    

    Essentially how I tried to get it working in functions.php:

    
    function member_avatars_to_profile() {
    
    NOTE: Basically copied the aforementioned code here but it returns nothing but errors. I tried stripping out the <?php and ?>'s, etc., but nothing I try works. 
    
    }
    add_action( 'theme_before_main', 'member_avatars_to_profile' );
    

    Really appreciate the help. I’m learning as I go and am going to learn a lot with this one. I’ve never really been able to grasp how to get certain things to work within the functions file so I’m looking forward to learning more about this process.

    Best Regards,
    Mark

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Need help converting PHP to run within my functions.php file.’ is closed to new replies.
Skip to toolbar