0

I haven't created a Magento module before and after a lot of research I attempted to create a module to insert wordpress avatars into blog posts inside Magento (I am using Fishpig).

Below I will list my folder structure and what I have in my files but I can't seem to get my custom module to work!

JUST TO NOTE: I am trying to extend "Fishpig_Wordpress_Model_User"

app/code/local/Splitpixel/Avatar/Fishpig/Wordpress/Model/User.php app/code/local/Splitpixel/Avatar/etc/config.xml

app/etc/modules/Splitpixel_Avatar.xml

My Splitpixel_Avatar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Splitpixel_Avatar>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </Splitpixel_Avatar>
    </modules>
</config>

My config.xml:

> <?xml version="1.0" encoding="UTF-8"?> <config>
>     <modules>
>         <Splitpixel_Avatar>
>             <version>0.0.1</version>
>         </Splitpixel_Avatar>
>     </modules>
>     <global>
>         <models>
>             <fishpig>
>                     <wordpress>
>                             <rewrite>
>                                 <user>Splitpixel_Avatar_Fishpig_Wordpress_Model_User</user>
>                             </rewrite>
>                     </wordpress>
>             </fishpig>
>         </models>
>     </global> </config>

My app/code/local/Splitpixel/Avatar/Fishpig/Wordpress/Model/User.php:

> <?php 
> 
> class Splitpixel_Avatar_Fishpig_Wordpress_Model_User extends
> Fishpig_Wordpress_Model_User {
>     public function getUserAvatar(){
>         $imageSource = “”;
>         $avatarPostId = $this->getMetaValue(‘wp_user_avatar’);
>         $collection = Mage::getResourceModel(‘wordpress/post_collection’)->addPostTypeFilter(‘attachment’)->addFieldToFilter(‘ID’, $avatarPostId);
>         foreach($collection as $post){
>             if ($post->getId() == $avatarPostId){
>                 $imageSource =  $post->getData(‘guid’);
>                 return $imageSource;
>             }
>         }
>         return $imageSource;
>     } }
> 
> ?>

And then finally I am trying to use the new function in "template/wordpress/post/meta.phtml" by using the following code:

>  ### WORKING CODE TO GET AUTHOR NAME ###
> $post->getAuthor()->getDisplayName()
> 
>  ### MY CODE THAT DOESN'T WORK ###
> $post->getAuthor()->getUserAvatar()

The code pulls back nothing! Now it may be my code, but the most likely factor is that I have dome something wrong in my config.xml or folder sructure as this is all new to me!

Any help is REALLY appreciated, thanks!

EDIT: Posted same time as patch drop so got missed by most!

2

Usually when extending module classes I trail the extension of a parent method for example _construct() - As far as extending User.php I can confirm the below works;

app/etc/modules/Splitpixel_Avatar.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Splitpixel_Avatar>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Fishpig_Wordpress />
            </depends>
        </Splitpixel_Avatar>
    </modules>
</config> 

app/code/local/Splitpixel/Avatar/Model/User.php

<?php
class Splitpixel_Avatar_Model_User extends Fishpig_Wordpress_Model_User
{

    public function _construct()
    {
        Mage::log(__METHOD__);
        return parent::_construct();
    }

}

app/code/local/Splitpixel/Avatar/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Splitpixel_Avatar>
            <version>0.0.1</version>
        </Splitpixel_Avatar>
    </modules>
    <global>
        <models>
            <wordpress>
                <rewrite>
                    <user>Splitpixel_Avatar_Model_User</user>
                </rewrite>
            </wordpress>
        </models>
    </global>
</config>

With regards to your function to get the avatar URL, I'm a little confused as to why you're not using the functionality provided within the module already, for example;

  • getGravatarUrl()
  • getUserPhoto() - "The UserPhoto plugin must be installed in WordPress"
  • Hi thanks for the help! I added that code to my user.php and it gave me a server error, is this correct? Just to test whether the module is extending the class? Unfortunately my code to echo the avater errors the whole page out so need to figure that out! – KiwisTasteGood Nov 5 '15 at 13:11
  • @KiwisTasteGood Sorry to hear you've got a server error, I've updated my reply to include a tested rewrite. – Dan Norris Nov 6 '15 at 9:54
  • Hi again, I could not find the getUserPhoto() anywhere when researching this but it works perfectly! Thank you :) – KiwisTasteGood Nov 9 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.