Reputation: 31
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?
Upvotes: 1
Views: 489
Reputation: 31
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.
}
}
Upvotes: 3