Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Turn Function into a Class

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #38299
    gilgimech
    Participant

    I’m trying to turn this function into a reusable class.


    function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }
    add_filter('the_content_more_link', 'more_jump_link');

    This is what I’ve come up with but it doesn’t work.


    class wp_more_jump_class {

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;
    }


    public function more_jump_link() {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }


    public function action_filters (){
    add_filter('the_content_more_link', $this->more_jump_link);
    }
    }

    $call_wp_more_jump = new wp_more_jump_class();
    $call_wp_more_jump->more_jump_link();

    I not sure why it doesn’t work I’ve made functions int class like this before and they worked.

    Anyone have any ideas on this?

    #103739
    Hawke
    Member

    What’s the problem exactly?

    From what I’ve seen so far, though :

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;
    }

    You put $link in more_jump_link, however you don’t pass it in your constructor, and you don’t have a set method. $link will then always be null. $link is not linked to any property in your class.

    #103741
    gilgimech
    Participant

    I’m not sure it just doesn’t work. Now If I have this.

    public function remove_more_jump_link($enable_rmvMoreJump) {
    function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }
    add_filter('the_content_more_link', 'more_jump_link');
    }

    It works just fine. I just don’t like having a function in a function like that. I’m trying to clean up my code, but I can’t get it to work.

    #103742
    Hawke
    Member

    See, in your function, you’re passing ($link) in the parameters of the function. However you never do in your class, thus $link is always null in the code.

    Also, in the constructor, you are doing this :

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;

    $link at this point is null, since you did not receive it in construct($link). Also, your more_jump_link function is defined as not receiving any parameter :


    public function more_jump_link() {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }

    You can see there that jump_link() is not waiting for the $link parameter you are passing in the constructor.

    Do something like this and tell me if it work :


    class wp_more_jump_class {
    function __construct()
    {
    }

    public function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }

    public function action_filters ()
    {
    add_filter('the_content_more_link', $this->more_jump_link);
    }
    }

    $call_wp_more_jump = new wp_more_jump_class();
    $call_wp_more_jump->more_jump_link($link);

    Note : action_filters will crash if you call it. You are trying to access a variable that does not exist.

    #103745
    gilgimech
    Participant

    Yeah, that doesn’t work. I’ve already been down that road. I think I’m just going to go back to using it as a plain old function. This is taking up too much of my time. I was just trying to work some OOP into my code.

    Thanks for your help anyways.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.