1

I would like to create a MySQL function in the user's database when they install my Wordpress plugin.

I've tried this code:

$wpdb->query(
"
DELIMITER $$
    CREATE FUNCTION `myfunc`(t TEXT CHARSET utf8) RETURNS TEXT CHARSET utf8
    BEGIN
        SET t = REPLACE(t, 'ą', 'a');
        SET t = REPLACE(t, 'Ą', 'A');
        SET t = REPLACE(t, 'ć', 'c');
        SET t = REPLACE(t, 'Ć', 'C');
        SET t = REPLACE(t, 'ę', 'e');
        SET t = REPLACE(t, 'Ę', 'E');
        SET t = REPLACE(t, 'ł', 'l');
        SET t = REPLACE(t, 'Ł', 'L');
        SET t = REPLACE(t, 'ń', 'n');
        SET t = REPLACE(t, 'Ń', 'N');
        SET t = REPLACE(t, 'ó', 'o');
        SET t = REPLACE(t, 'Ó', 'O');
        SET t = REPLACE(t, 'ś', 's');
        SET t = REPLACE(t, 'Ś', 'S');
        SET t = REPLACE(t, 'ż', 'z');
        SET t = REPLACE(t, 'Ż', 'Z');
        SET t = REPLACE(t, 'ź', 'z');
        SET t = REPLACE(t, 'Ź', 'Z');
        return t;
    END
"
);

but it doesn't work, the function is not created. Is there any other way to insert this function from a Wordpress plugin?

4
  • Does any query get run by this command? Does this query process properly if you enter it into MySQL directly? Have you checked if the database user has CREATE_ROUTINE privileges? Commented Nov 27, 2015 at 18:46
  • yes, if i login to phpmyadmin and run it query(as same user) it works perfectly Commented Nov 27, 2015 at 18:47
  • Ok, what is the context of this code? What file is it in, how is it being called? Please edit your answer to include more of the code. Also ensure you're logging or displaying errors, and provide them here. Commented Nov 27, 2015 at 18:50
  • 1
    Also have you tried removing the DELIMITER statement? This question had similar problems, but went unanswered. The DELIMITER statement is used on the command line but shouldn't be necessary when sending things from PHP. Commented Nov 27, 2015 at 18:54

1 Answer 1

3

CREATE FUNCTION is not supported by dbDelta function. It only supports the following SQL statements:

CREATE TABLE
CREATE DATABASE
INSERT INTO
UPDATE

Here is code snippet from dbDelta function

https://core.svn.wordpress.org/trunk/wp-admin/includes/upgrade.php

foreach ( $queries as $qry ) {
    if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
        $cqueries[ trim( $matches[1], '`' ) ] = $qry;
        $for_update[ $matches[1] ]            = 'Created table ' . $matches[1];
    } elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
        array_unshift( $cqueries, $qry );
    } elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
        $iqueries[] = $qry;
    } elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
        $iqueries[] = $qry;
    } else {
        // Unrecognized query type.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.