0
Welcome Guest! Login
0 items Join Now

"Copying" a widget with slight code changes

  • "Copying" a widget with slight code changes

    Posted 10 years 9 months ago
    • Hello Again,

      This time im calling for your help in this situation, i want to create a copy of a widget, so in one i have one widget coded text, and in the other i have a different coded text, the widget is a popup login, what i did here was editing the loginform.php and creating a loginform2.php what happens though is when i go to the widgets section in gantry, the page formatting is completely gone, everything is out of place and i cant add or remove widgets until i delete loginform2.php

      my question is, the widget is composed by:

      loginform.php
      <?php
      /**
       * @version   ${project.version} ${build_date}
       * @author     RocketTheme http://www.rockettheme.com
       * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
       * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
       */
      defined('GANTRY_VERSION') or die();
      gantry_import('core.gantrywidget');
      add_action('widgets_init', array("GantryWidgetLoginForm","init"));
      class GantryWidgetLoginForm extends GantryWidget {
           var $short_name = 'loginform';
           var $wp_name = 'gantry_loginform';
           var $long_name = 'Gantry Login Form';
           var $description = 'Gantry Login Form Widget';
           var $css_classname = 'widget_gantry_loginform';
           var $width = 200;
           var $height = 400;
           function init() {
                register_widget("GantryWidgetLoginForm");
           }
           function render_title($args, $instance) {
                global $gantry;
                if($instance['title'] != '') :
                     echo $instance['title'];
                endif;
           }
           function render($args, $instance){
                global $gantry, $current_user;
                ob_start();
                ?>
                <?php if(!is_user_logged_in()) : ?>
                     <form action="<?php echo wp_login_url($_SERVER['REQUEST_URI']); ?>" method="post" id="login-form">
                          <?php if ($instance['pretext'] != ''): ?>
                          <div class="pretext">
                               <p><?php echo $instance['pretext']; ?></p>
                          </div>
                          <?php endif; ?>
                          <fieldset class="userdata">
                               <p id="form-login-username">
                                    <label for="modlgn-username"><?php _re('Utilizador'); ?></label>
                                    <input id="modlgn-username" type="text" name="log" class="inputbox" alt="username" size="18" value="" />
                               </p>
                               <p id="form-login-password">
                                    <label for="modlgn-passwd"><?php _re('Palavra-Passe'); ?></label>
                                    <input id="modlgn-passwd" type="password" name="pwd" class="inputbox" size="18" alt="password" value="" />
                               </p>
                               <p id="form-login-remember">
                                    <label for="modlgn-remember"><?php _re('Lembrar-me'); ?></label>
                                    <input id="modlgn-remember" type="checkbox" name="rememberme" class="inputbox" />
                               </p>
                               <input type="submit" value="<?php _re('Entrar'); ?>" class="button" name="submit" />
                          </fieldset>                
                          <ul>
                               <li>
                                    <a href="http://www.gebox.biz/wp-login.php?action=lostpassword"><?php _re('Esqueceu a Palavra Passe?'); ?></a>
                               </li>
                               <?php if(get_option('users_can_register')) : ?>
                               <li>
                                    <a href="http://www.gebox.biz/wp-login.php?action=register"><?php _re('Registar'); ?></a>
                               </li>
                               <?php endif; ?>
                          </ul>
                          <?php if ($instance['posttext'] != ''): ?>
                          <div class="posttext">
                               <p><?php echo $instance['posttext']; ?></p>
                          </div>
                          <?php endif; ?>                
                     </form>
                <?php else : ?>
                     <form action="<?php echo wp_logout_url($_SERVER['REQUEST_URI']); ?>" method="post" id="login-form">
                          <div class="login-greeting">
                               <p><?php echo $instance['user_greeting']; ?> <?php echo $current_user->display_name; ?></p>
                          </div>
                          <div class="logout-button">
                               <input type="submit" name="Submit" class="button" value="<?php _re('Sair'); ?>" />
                          </div>
                     </form>
                <?php endif; ?>
                <?php 
                echo ob_get_clean();
           }
      }

      loginform.xml
      <?xml version="1.0" encoding="utf-8"?>
      <!-- $Id: style.xml 16565 2010-04-28 12:21:27Z ian $ -->
      <form>
           <fieldset name="widget">
                <field name="title" type="text" label="Title" description="Title" default="Login Form" class="widefat" readonly="false"/>
                <field name="user_greeting" type="text" label="User Greeting" description="Text to be displayed as an user greeting" default="Hi," class="widefat" readonly="false"/>
                <field name="pretext" type="text" label="Pre-text" description="Text to be displayed before the login form" default="" class="widefat" readonly="false"/>
                <field name="posttext" type="text" label="Post-text" description="Text to be displayed after the login form" default="" class="widefat" readonly="false"/>
           </fieldset>
      </form>

      chrome_login.php
      <?php
      /**
       * @version   ${project.version} ${build_date}
       * @author     RocketTheme http://www.rockettheme.com
       * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
       * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
       *
       */
      defined('GANTRY_VERSION') or die();
      gantry_import('core.gantrylayout');
      /**
       *
       * @package gantry
       * @subpackage html.layouts
       */
      class GantryLayoutWidget_Login extends GantryLayout {
           var $render_params = array(
                'gridCount'      =>  null,
                'prefixCount'   =>  0,
                'extraClass'       =>  ''
           );
           function render($params = array()){
                global $gantry;
                $params = $gantry->renderLayout("chrome_".$params[0]['chrome'], $params);
                $params[0]['position_open'] ='';
                $params[0]['position_close'] ='';
                $rparams = $this->_getParams($params[0]);
                $start_tag = "";
                // see if this is the first widget in the postion
                if (property_exists($rparams,'start') && $rparams->start == $rparams->widget_id) {
                     ob_start();
                     ?>
                     <div id="rt-popuplogin">
                     <?php
                     $start_tag = ob_get_clean();
                     $params[0]['position_open'] = $start_tag;
                }
                if (property_exists($rparams,'end') && $rparams->end == $rparams->widget_id) {
                      $params[0]['position_close'] = "</div>";
                }
                $params[0]['before_widget'] = $params[0]['position_open'].$params[0]['before_widget'] ;
                $params[0]['after_widget'] = $params[0]['after_widget'] . $params[0]['position_close'];
                return $params;
           }
      }

      widget_login.php
      <?php
      /**
       * @version   ${project.version} ${build_date}
       * @author     RocketTheme http://www.rockettheme.com
       * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
       * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
       *
       */
      defined('GANTRY_VERSION') or die();
      gantry_import('core.gantrylayout');
      /**
       *
       * @package gantry
       * @subpackage html.layouts
       */
      class GantryLayoutWidget_Login extends GantryLayout {
           var $render_params = array(
                'gridCount'      =>  null,
                'prefixCount'   =>  0,
                'extraClass'       =>  ''
           );
           function render($params = array()){
                global $gantry;
                $params = $gantry->renderLayout("chrome_".$params[0]['chrome'], $params);
                $params[0]['position_open'] ='';
                $params[0]['position_close'] ='';
                $rparams = $this->_getParams($params[0]);
                $start_tag = "";
                // see if this is the first widget in the postion
                if (property_exists($rparams,'start') && $rparams->start == $rparams->widget_id) {
                     ob_start();
                     ?>
                     <div id="rt-popuplogin">
                     <?php
                     $start_tag = ob_get_clean();
                     $params[0]['position_open'] = $start_tag;
                }
                if (property_exists($rparams,'end') && $rparams->end == $rparams->widget_id) {
                      $params[0]['position_close'] = "</div>";
                }
                $params[0]['before_widget'] = $params[0]['position_open'].$params[0]['before_widget'] ;
                $params[0]['after_widget'] = $params[0]['after_widget'] . $params[0]['position_close'];
                return $params;
           }
      }

      loginbutton.php
      <?php
      /**
       * @version   ${project.version} ${build_date}
       * @author     RocketTheme http://www.rockettheme.com
       * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
       * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
       */
      defined('GANTRY_VERSION') or die();
      gantry_import('core.gantrywidget');
      add_action('widgets_init', array("GantryWidgetLoginButton","init"));
      class GantryWidgetLoginButton extends GantryWidget {
           var $short_name = 'loginbutton';
           var $wp_name = 'gantry_loginbutton';
           var $long_name = 'Gantry Login Button';
           var $description = 'Gantry Login Button Widget';
           var $css_classname = 'widget_gantry_loginbutton';
           var $width = 200;
           var $height = 400;
           function init() {
                register_widget("GantryWidgetLoginButton");
           }
           function render_widget_open($args, $instance) {
           }
           function render_widget_close($args, $instance) {
           }
           function pre_render($args, $instance) {
           }
           function post_render($args, $instance) {
           }
           function render_title($args, $instance) {
                global $gantry;
                if($instance['title'] != '') :
                     echo $instance['title'];
                endif;
           }
           function render($args, $instance){
                global $gantry, $current_user;
                ob_start();
                ?>
                <div id="<?php echo $this->id; ?>" class="widget <?php echo $this->css_classname; ?> rt-block">
                     <div class="rt-popupmodule-button">
                     <?php if(!is_user_logged_in()) : ?>
                          <a href="#" class="buttontext button" rel="rokbox[355 385][module=rt-popuplogin]">
                               <span class="desc"><?php echo $instance['logintext']; ?></span>
                          </a>
                     <?php else : ?>
                          <a href="http://www.gebox.biz/wp-login.php?loggedout=true" class="buttontext button">
                               <span class="desc"><?php echo $instance['logouttext']; ?> <?php echo $current_user->display_name; ?></span>
                          </a>
                     <?php endif; ?>
                     </div>
                </div>
                <?php 
                echo ob_get_clean();
           }
      }

      and i edited:
      templateDetails
      template.less

      basically i did everything like explained in the login widget tutorial by gantry

      what i did to try my solution was creating a second loginform.php changing every widget name and every reference that cant exist in double and then i did the changes in the code that i wanted.

      What am i doing wrong? did i change something i shouldnt have? do i have to create another version of every file to have the existence of 2 separate widgets that do the same but with some differences?

      thanks in advance
      Antero Duarte
    • Shazdeh's Avatar
    • Shazdeh
    • Elite Rocketeer
    • Posts: 4984
    • Thanks: 29

    Re: "Copying" a widget with slight code changes

    Posted 10 years 9 months ago
    • Hi,
      the problem is that when you copy the widget's files and declare a new widget, PHP throws "class already exists" errors and thus messes up the admin page. After copying files you need to rename all class names and fix their references.
      Mind me asking, why a new copy of the login form widget? You can instead add an option to the loginform.xml file and edit the loginform.php file to modify the widget's behavior based on that option.
    • Please don't PM unless requested ;)
      Consider using the custom stylesheet for all your CSS customizations: gantry-framework.org/documentation/wordp...custom_stylesheet.md .
      Please provide the URL to the issue.
      And please mark the thread as solved if the issue is resolved.
  • Re: "Copying" a widget with slight code changes

    Posted 10 years 9 months ago
    • Hello, thanks for the response,

      to be really honest, copying the widget is only because i dont really know that much about php, im just entering this world, so, copying the widget was the only thing i could think about, can you help me with changing the .xml so i can edit every aspect of the login form? what i need is a way to alter the "register","forgot password","remember me","username" and "password" link's text in the widget customization form, instead of changing it directly in the code, that would be absolutely awesome, as i am also thinking of expanding to new languages and it would be perfect for that case...

      Thanks in advance,
      Antero Duarte
    • Shazdeh's Avatar
    • Shazdeh
    • Elite Rocketeer
    • Posts: 4984
    • Thanks: 29

    Re: "Copying" a widget with slight code changes

    Posted 10 years 9 months ago
    • Hi,
      Gantry makes adding new fields to widgets very easy. All you need to do is to modify the widget's .xml file and add a new field:
      <field name="my_field_name" type="text" label="My Custom Field" default="" class="widefat" readonly="false" />
      You must give the field a unique name (it's "my_field_name" here). So now if you go to the Widgets manager you see that the widget has this new "My Custom Field" option. Now to display that value in the widget output you need to modify the widget's .php file and add:
      <?php echo $instance['my_field_name']; ?>
      where you want that text to appear. And done.
    • Please don't PM unless requested ;)
      Consider using the custom stylesheet for all your CSS customizations: gantry-framework.org/documentation/wordp...custom_stylesheet.md .
      Please provide the URL to the issue.
      And please mark the thread as solved if the issue is resolved.
  • Re: "Copying" a widget with slight code changes

    Posted 10 years 9 months ago
    • thank you so much, sorry for bothering, if i have any further needs for help, i will post here again, but i think i can get along with what you gave me :)

      Edit: Worked like a charm, thank you so much for your support :)

Time to create page: 0.043 seconds