MyBB Community Forums

Full Version: Replace text in first post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I am making a plugin it will replace a text with some data which i will get from another site's api. But i have a problem how can i replace message i write plugin but doesn't work. Probably wrong hook.

Code:

<?php
$plugins->add_hook("parse_message", "serveronline_replace");

function serveronline_replace(&$post) {
    global $db, $mybb, $thread;

    if(basename($_SERVER['PHP_SELF']) == "showthread.php" && $thread['closed'] != 1 && $post['pid'] == $thread['firstpost'] && $mybb->user['uid'] == $thread['uid']) {
 $post['cevrim'] = "5";
 //Other codes
    }else{
 $post['cevrim'] = "0";
 }
 
}

?>


[Image: image.png]

As seen it doesn't replace. And due to hook i can't detect first post ($post['pid'] throws error)

Thanks.
use the hook 'postbit' and test the post with if ($thread['firstpost'] == $post['pid']) {
$thread is a global and $post is arg of the function (passed by reference).
(2019-05-05, 10:04 PM)Crazycat Wrote: [ -> ]use the hook 'postbit'  and test the post with if ($thread['firstpost'] == $post['pid']) {
$thread is a global and $post is arg of the function (passed by reference).

I already test with the postbit it doesn't replace.
Tested right now:
<?php

if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function firstpost_info() {
    return array(
        'name' => 'firstpost',
        'description' => 'Test firstpost',
        'website' => 'https://community.mybb.com',
        'author' => 'CrazyCat',
        'authorsite' => 'http://community.mybb.com/mods.php?action=profile&uid=6880',
        'version' => '0.1',
        'compatibility' => '18*',
        'codename' => 'firstpost'
    );
}

$plugins->add_hook('postbit', 'firstpost_do');

function firstpost_do(&$post) {
    global $thread;
    if ($thread['firstpost'] == $post['pid']) {
        $post['message'] = 'I am a first post<br />'.$post['message'];
    }
}

And the first post of each thread is well displayed with "I am a first post" at the beginning.
(2019-05-06, 06:19 AM)Crazycat Wrote: [ -> ]Tested right now:
<?php

if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function firstpost_info() {
    return array(
        'name' => 'firstpost',
        'description' => 'Test firstpost',
        'website' => 'https://community.mybb.com',
        'author' => 'CrazyCat',
        'authorsite' => 'http://community.mybb.com/mods.php?action=profile&uid=6880',
        'version' => '0.1',
        'compatibility' => '18*',
        'codename' => 'firstpost'
    );
}

$plugins->add_hook('postbit', 'firstpost_do');

function firstpost_do(&$post) {
    global $thread;
    if ($thread['firstpost'] == $post['pid']) {
        $post['message'] = 'I am a first post<br />'.$post['message'];
    }
}

And the first post of each thread is well displayed with "I am a first post" at the beginning.
Thank you very much. How can i replace text in that message? I tried:

$post['message'] = str_replace('$post[\'cevrim\']', 'test', $post['message']);
but didn't work
What is $post['cevrim'] ? an additionnal field in your post ? Someting coming from a plugin ? Do you got it when doing a var_dump($post) ?
I need some informations to help you
Its just a text im using for test frankly it will something like this [online]number[/online] i will get this number and replace with something.
Ok, so try:
$post['message'] = str_replace($post['cevrim'], 'test', $post['message']);
In your previous code, you were trying to replace the string "$post['cevrim']", not the content of the variable.
(2019-05-07, 04:49 AM)Crazycat Wrote: [ -> ]Ok, so try:
$post['message'] = str_replace($post['cevrim'], 'test', $post['message']);
In your previous code, you were trying to replace the string "$post['cevrim']", not the content of the variable.

No it is a string like i said just a text it can be anything but it doesn't get replace.
I don't understand what is your trouble.

My code:
<?php

if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function firstpost_info() {
    return array(
        'name' => 'firstpost',
        'description' => 'Test firstpost',
        'website' => 'https://community.mybb.com',
        'author' => 'CrazyCat',
        'authorsite' => 'http://community.mybb.com/mods.php?action=profile&uid=6880',
        'version' => '0.1',
        'compatibility' => '18*',
        'codename' => 'firstpost'
    );
}

$plugins->add_hook('postbit', 'firstpost_do');

function firstpost_do(&$post) {
    global $thread;
    if ($thread['firstpost'] == $post['pid']) {
        $post['message'] = str_replace('[online]15[/online]', 'You are 15 online', $post['message']);
    }
}

The result when the plugin isn't activated: [attachment=41759]

The result when I activate it: [attachment=41760]

You can see that the replacement is well done in the first post, so your trouble is in your str_replace().
Pages: 1 2